Problem
You are given an array of CPU tasks
, each represented by letters A to Z, and a cooling time, n
. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: identical tasks must be separated by at least n
intervals due to cooling time.
Return the minimum number of intervals required to complete all tasks.
Example 1:
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.
After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th cycle, you can do A again as 2 intervals have passed.
Example 2:
Input: tasks = ["A","C","A","B","D","B"], n = 1
Output: 6
Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.
With a cooling interval of 1, you can repeat a task after just one other task.
Example 3:
Input: tasks = ["A","A","A", "B","B","B"], n = 3
Output: 10
Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.
There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.
Intuition
The problem involves scheduling CPU tasks with cooling intervals. The objective is to minimize the number of intervals required to complete all tasks. The idea is to keep track of the count of each task and use a priority queue (max-heap) to prioritize the tasks with the highest count. Additionally, a deque is used to keep track of tasks that have been processed but need to wait for the cooling time before being eligible again.
Approach
Count Tasks:
Count the occurrences of each task in the tasks array using a Counter.
Max-Heap Initialization:
Create a max-heap (maxheap) with the negation of counts, as heapq only supports min-heaps.
The heap will contain the negation of counts, ensuring that tasks with higher counts are processed first.
Simulation:
Use a simulation approach to process tasks and cooling intervals.
Initialize a time variable to track the current time.
Use a deque (q) to keep track of tasks that have been processed but are waiting for the cooling time.
Continue processing tasks until both the max-heap and deque are empty.
Processing Tasks:
At each time step, process a task from the max-heap (tasks with higher counts are processed first).
If the count of the task is greater than 1, add a tuple [count - 1, time + n] to the deque.
Decrement the count of the processed task and push it back onto the max-heap.
Cooling Time:
Check the deque for tasks that need to wait for the cooling time to expire.
If the time in the deque matches the current time, pop the task from the deque and push it back onto the max-heap.
Result:
The result is the total time taken to process all tasks.
Complexity
- Time complexity:
The time complexity is O(N log N), where N is the number of tasks. Heap operations and deque operations are performed for each task.
- Space complexity:
The space complexity is O(D), where D is the number of distinct tasks. The max-heap and deque store information about each distinct task.
Code
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
count = Counter(tasks)
maxheap = [-cnt for cnt in count.values()]
heapq.heapify(maxheap)
time = 0
q = deque()
while maxheap or q:
time += 1
if maxheap:
cnt = 1 + heapq.heappop(maxheap)
if cnt:
q.append([cnt, time + n])
if q and q[0][1] == time:
heapq.heappush(maxheap, q.popleft()[0])
return time