Task Scheduler

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
  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值