Task Scheduler
MediumHeap
You are given a list of CPU tasks, each labeled with an uppercase letter, and a non-negative integer cooldown n.
Time is divided into intervals. In each interval the CPU either runs one task or stays idle. Tasks can be run in any order, and every task takes exactly one interval.
The cooldown rule: two runs of tasks with the same label must be separated by at least n intervals (those intervals can hold other tasks or idle time). Tasks with different labels have no restriction.
Return the minimum total number of intervals (including idle ones) needed to run every task.
Example 1
- Input:
- tasks = ["A", "A", "A", "B", "C"], n = 3
- Output:
- 9
- Explanation
- Each
Amust be followed by at least 3 other intervals before the nextA. One optimal schedule isA B C idle A idle idle idle A, which takes 9 intervals.
Example 2
- Input:
- tasks = ["M", "M", "M", "N", "N", "N", "O"], n = 2
- Output:
- 8
- Explanation
MandNboth appear 3 times. ScheduleM N O M N idle M N: every repeat is 2 intervals apart, and one idle slot is unavoidable, giving 8.
Example 3
- Input:
- tasks = ["X", "X", "Y", "Y", "Z"], n = 1
- Output:
- 5
- Explanation
X Y X Y Z(orX Y Z X Y) keeps every repeat at least 1 interval apart with no idle time, so the answer is just the number of tasks, 5.
Constraints
1 <= tasks.length <= 10^4tasks[i]is an uppercase English letter0 <= n <= 100