Find the minimum CPU intervals to run tasks with a cooldown between identical tasks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Given tasks labeled by uppercase letters and a cooldown n between runs of the same task, compute the fewest CPU intervals needed to finish them all, idle slots included. It tests greedy reasoning from task counts, proving that a lower bound can be reached, and edge cases such as a zero cooldown.
Constraints
- 1 <= len(tasks) <= 10^4
- 0 <= n <= 100
- Every tasks[i] is a single uppercase English letter from A to Z.
Examples
Input: (['A', 'A', 'A', 'B', 'B', 'B'], 2)
Expected Output: 8
Explanation: Source example: A B idle A B idle A B. A and B tie at count 3, so (3-1)*3+2 = 8.
Input: (['A', 'A', 'A', 'B', 'B', 'B'], 0)
Expected Output: 6
Explanation: Source example with no cooldown: the tasks run back to back, so the answer is the task count 6.
Hints
- Look at the task kind that appears most often. How many intervals would it need if it were the only task?
- If several task kinds share the highest count, think about what the final stretch of the schedule must contain.
- Try a case with many different letters and a small cooldown, where the schedule may need no idle intervals at all.