Task Scheduler
Given a list of CPU tasks labeled by letters and an integer n, each task takes one unit of time and identical tasks must be separated by at least n units of cooldown. In each unit the CPU either runs one task or stays idle. Return the minimum number of time units needed to finish all tasks.
Open official problem prompt ↗Compute the shortest possible schedule length that runs every task exactly once while never repeating a task within n units, inserting idle slots only when unavoidable.
Seating repeated guests at a long table where the same family must be at least n chairs apart. The largest family sets the spacing; smaller families fill the chairs in between, and you only leave chairs empty when there simply are not enough other guests to fill the required gaps.
- Input
- tasks = ["A","A","A","B","B","B"], n = 2
- Output
- 8
- Why
- A valid schedule is A B idle A B idle A B, taking 8 units — the two idles are forced by the cooldown of 2 between repeats of A.
1 <= tasks.length <= 10^4tasks[i] is an uppercase English letter0 <= n <= 100