[LeetCode] 621. Task Scheduler

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.
You need to return the least number of intervals the CPU will take to finish all the given tasks.
Example 1:
Input: tasks = ['A','A','A','B','B','B'], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
Note:
  1. The number of tasks is in the range [1, 10000].
  2. The integer n is in the range [0, 100].

Thought process:
The tasks with higher numbers of instances should be executed before the ones with lower numbers of instances. The names of the tasks don't matter. Only their numbers of instances matter, so I can map the tasks  to an array of size 26 and sort it without worrying about mapping the tasks to their counts.
The tasks with higher priority (more instances) should be interleaved correctly with the ones with lower priority. This means that as soon as an iteration cycle (the number of iterations reaches cooling time n) is over, I need to sort the array again and execute the new task with the highest priority.
All tasks might be finished in the middle of an iteration cycle. If the task with the highest priority is already finished, then I can break out of the loop early.

Solution 1 (sorting):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];
        for (char task : tasks) {
            count[task - 'A']++;
        }
        Arrays.sort(count);
        
        int intervals = 0;
        while (count[25] > 0) {
            for (int i = 0; i <= n; i++) {
                intervals++;
                if (i > 25) {
                    continue;
                }
                int index = 25 - i;
                if (count[index] > 0) {
                    count[index]--;
                }
                if (count[index] == 0 && count[25] == 0) {
                    break;
                }
            }
            Arrays.sort(count);
        }
        return intervals;
    }
}

The same idea can be implemented with a heap.

Solution 2 (heap):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
    public int leastInterval(char[] tasks, int n) {
        int[] count = new int[26];
        for (char task : tasks) {
            count[task - 'A']++;
        }
        
        Queue<Integer> heap = new PriorityQueue<>(Collections.reverseOrder());
        for (int i : count) {
            if (i > 0) {
                heap.offer(i);
            }
        }
        
        int intervals = 0;
        while (!heap.isEmpty()) {
            int i = 0;
            List<Integer> next = new ArrayList<>();
            
            while (i <= n) {
                if (!heap.isEmpty()) {
                    if (heap.peek() > 1) {
                        next.add(heap.poll() - 1);
                    } else {
                        heap.poll();
                    }
                }
                i++;
                intervals++;
                if (heap.isEmpty() && next.size() == 0) {
                    break;
                }
            }
            
            for (int task : next) {
                heap.offer(task);
            }
        }
        return intervals;
    }
}

Time complexity:
Sorting takes O(1) because length of the array is fixed. The loops take O(intervals). So the overall time complexity is O(intervals).

Comments

Popular posts from this blog

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 269. Alien Dictionary

[LeetCode] 631. Design Excel Sum Formula