Task scheduler
Challenge · Write code · about 25 min
A CPU runs one task per time unit. tasks lists the tasks by letter, and the same letter can appear many times. Two runs of the same letter need at least n other time units between them. A time unit can be idle.
Return the fewest time units needed to run every task. Greedy works: at each time unit, run the ready task with the most copies left.
Example 1
Input:
[["A","A","A","B","B","B"],2]
Output:
8
A, B, idle, A, B, idle, A, B takes 8 units.
Example 2
Input:
[["A","C","A","B","D","B"],1]
Output:
6
No idle slot is needed.
Constraints
- 1 ≤ tasks.length ≤ 10,000
- Each task is an uppercase letter A to Z
- 0 ≤ n ≤ 100
Learn the idea first: Priority queues in practice: top-k, k-way merge, scheduling, a visual lesson.
More Priority queues in practice: top-k, k-way merge, scheduling problems
- Which heap keeps the top k? Warm-up
- K largest values Warm-up
- Last stone weight Warm-up
- Merge k sorted lists Standard
- Top k frequent values Standard
- Trace a size-k min-heap Standard