Advertisement
1988coder

621. Task Scheduler

Nov 21st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. /*
  2. Time Complexity: O(M * logM)
  3. Space Complexity: O(M)
  4. M = length of the tasks array
  5.  
  6. When printing the Ordered string.. Time Complexity: O(M * logM + Idle Slots)
  7. */
  8. class Solution {
  9.     public int leastInterval(char[] tasks, int n) {
  10.         if (tasks == null || tasks.length == 0) {
  11.             return 0;
  12.         }
  13.         if (n == 0 || tasks.length == 1) {
  14.             return tasks.length;
  15.         }
  16.        
  17.         HashMap<Character, Integer> map = new HashMap<>();
  18.         for (int i = 0; i < tasks.length; i++) {
  19.             map.put(tasks[i], map.getOrDefault(tasks[i], 0) + 1);
  20.         }
  21.        
  22.         PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<Map.Entry<Character, Integer>>(new Comparator<Map.Entry<Character, Integer>>() {
  23.             public int compare(Map.Entry<Character, Integer> a, Map.Entry<Character, Integer> b) {
  24.                 return b.getValue() - a.getValue();
  25.             }
  26.         });    
  27.        
  28.         queue.addAll(map.entrySet());
  29.        
  30.         int currTime = 0;
  31.         // StringBuilder sb = new StringBuilder();
  32.        
  33.         while (!queue.isEmpty()) {
  34.             int k = n + 1;
  35.             ArrayList<Map.Entry<Character, Integer>> tempList = new ArrayList<>();
  36.             while (k > 0 && !queue.isEmpty()) {
  37.                 Map.Entry<Character, Integer> topEntry = queue.poll();
  38.                 topEntry.setValue(topEntry.getValue() - 1);
  39.                 // sb.append(topEntry.getKey());
  40.                 tempList.add(topEntry);
  41.                 k--;
  42.                 currTime++;
  43.             }
  44.            
  45.             for (Map.Entry<Character, Integer> entry : tempList) {
  46.                 if(entry.getValue() > 0) {
  47.                     queue.offer(entry);
  48.                 }
  49.             }
  50.            
  51.             if (!queue.isEmpty()) {
  52.                 currTime += k;
  53.                 // for (int i = 0; i < k; i++) {
  54.                 //     sb.append(" ");
  55.                 // }
  56.             }
  57.         }
  58.         // System.out.println(sb.toString());
  59.         return currTime;
  60.     }
  61. }
  62.  
  63.  
  64. /*
  65. Refer: https://leetcode.com/problems/task-scheduler/discuss/104496/concise-Java-Solution-O(N)-time-O(26)-space
  66.  
  67. Time Complexity: O(n)
  68. Space Complexity: O(1)
  69. */
  70. class Solution {
  71.     public int leastInterval(char[] tasks, int n) {
  72.         if (tasks == null || tasks.length == 0) {
  73.             return 0;
  74.         }
  75.         if (n == 0 || tasks.length == 1) {
  76.             return tasks.length;
  77.         }
  78.  
  79.         int[] counts = new int[26];
  80.         for (char task : tasks) {
  81.             counts[task - 'A']++;
  82.         }
  83.         Arrays.sort(counts);
  84.         int i = 25;
  85.        
  86.         while (i >= 0 && counts[i] == counts[25]) {
  87.             i--;
  88.         }
  89.        
  90.         /*
  91.         (counts[25]-1) = Number of frames = Highest frequency - 1
  92.         (n+1) = Frame Size
  93.         (25-i) = Number of delimiters or Number of tasks with highest frequency.
  94.         */
  95.         return Math.max(tasks.length, (counts[25]-1) * (n+1) + (25-i));
  96.     }
  97. }
  98.  
  99.  
  100. /*
  101. If order has to be maintained, follow below soultion.
  102.  
  103. Time Complexity: O(M)
  104. Space Complexity: O(M)
  105. M = length of tasks array
  106. */
  107. class Solution {
  108.     public int leastInterval(char[] tasks, int n) {
  109.         if (tasks == null || tasks.length == 0) {
  110.             return 0;
  111.         }
  112.         if (n == 0 || tasks.length == 1) {
  113.             return tasks.length;
  114.         }
  115.        
  116.         HashMap<Character, Integer> map = new HashMap<>();
  117.         int currTime = 0;
  118.        
  119.         int i = 0;
  120.         while (i < tasks.length) {
  121.             currTime++;
  122.            
  123.             if (!map.containsKey(tasks[i]) || currTime - map.get(tasks[i]) > n) {
  124.                 map.put(tasks[i], currTime);
  125.                 i++;
  126.             }
  127.         }
  128.        
  129.         return currTime;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement