Example 1
- Input
- tasks = ["A","A","A","B","B","B"], n = 2
- Output
- 8
One best order is A -> B -> idle -> A -> B -> idle -> A -> B, which keeps two intervals between equal letters.
Given tasks labeled A through Z and a cooldown n forcing identical tasks at least n intervals apart, arrange every task into the shortest schedule that may include idle slots. Return that minimum total interval count.
Given a list of tasks tasks where each entry is an uppercase letter, and a cooldown integer n, schedule every task on a CPU that handles one task or one idle slot per interval.
Between consecutive executions of the same letter, at least n complete intervals must pass, while different tasks may run back to back in any order.
Return the smallest possible total number of intervals, counting both work and idle time.
1 <= tasks.length <= 10^4tasks[i] is an uppercase English letter.0 <= n <= 100One best order is A -> B -> idle -> A -> B -> idle -> A -> B, which keeps two intervals between equal letters.
With no cooldown, any order works, for example A -> A -> A -> B -> B -> B, so no idle slot is needed.
One best order is A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A.
Repeated letters, rather than task order, force the idle time. In Example 1, three copies of A need two gaps of two intervals; B fills one interval in each gap but cannot fill both because B also has a cooldown. A heap can simulate the schedule one interval at a time. Counting the largest frequency and how many letters share it gives the same minimum without constructing the schedule.
Reveal Cooldown Simulation: intuition, complexity, and code
maxFreq - 1 blocks of n + 1 slots each, with the other top tasks filling the tail.tasks.length.n = 0 removes every cooldown, so any order works and the answer is exactly tasks.length.n = 2, also needs zero idle time.A with n = 2, forces maximum idle: five width-3 gaps plus a final A give 16 intervals.tasks.length, which undercounts when the cooldown is tiny.maxCount, which drops the tail slots of tied tasks.Cooldown schedules are won by serving the highest remaining frequency first; when only the shape of the answer matters, the most frequent item often yields a closed-form frame.