You're given a list of tasks to run on a single CPU and a cooldown rule: after a task runs, the same task can't run again until at least cooldown other time units have passed. Each task takes exactly one unit of time. In any unit the CPU may run a different task or simply sit idle. Your job is to return the minimum number of time units — counting idles — needed to finish every task. This is the classic Task Scheduler problem; the inputs are task identifiers (often single characters) and a cooldown count.
// tasks: string[] — task identifiers, e.g. ['A', 'A', 'A', 'B', 'B', 'B'].
// Repeats are allowed; that's the whole point.
// cooldown: number — minimum number of intervals between two runs of the
// SAME task. cooldown = 2 means at least two other slots
// (other tasks or idles) must sit between two A's.
// returns: number — the fewest time units, idles included, to run them all.
function taskCoordination(tasks, cooldown): number;
// A appears 3×, B appears 3×, cooldown 2.
// One optimal order: A B _ A B _ A B (two forced idles).
taskCoordination(['A', 'A', 'A', 'B', 'B', 'B'], 2);
// → 8
// Only one task, repeated, with cooldown 2.
// Nothing else can fill the gaps, so the CPU idles: A _ _ A _ _ A.
taskCoordination(['A', 'A', 'A'], 2);
// → 7
cooldown = 0 means no gap at all. With no cooldown the tasks just run back-to-back, so the answer is exactly tasks.length.