Skip to content
BytePatterns

Task Scheduler Cooldown

MediumHeaps#heap#greedy~35m

Problem

A processor runs one task per time slot. Two runs of the same task must be separated by at least gap slots, during which the processor may run a different task or sit idle. Given the list of tasks to run in any order, return the smallest number of slots — busy and idle together — needed to finish them all.

Examples

Input:  tasks = ["a", "a", "a", "b", "b", "b"], gap = 2
Output: 8
Why:    a b _ a b _ a b fills six tasks into eight slots
Input:  tasks = ["a", "a", "a"], gap = 2
Output: 7
Why:    a _ _ a _ _ a — nothing else exists to fill the waits
Input:  tasks = ["a", "b", "c"], gap = 0
Output: 3
Why:    edge case, no cooldown means no idling

Hints

0 / 3

Stuck on the idea rather than the code? Task Scheduler covers it.