Shortest schedule with a cooldown
A processor must run every task in tasks, a string where each letter names a task; each run takes one time unit. Between two runs of the same letter there must be at least gap other units, busy or idle: a letter run at time t can run again at time t + gap + 1 at the earliest. In each unit the processor runs one task or stays idle, and it may take the tasks in any order. Return the fewest units it needs to run them all.
tasks holds uppercase letters and may be empty; gap >= 0 and can be large. Aim for O(n) time, independent of gap.
Besides the tests, one large input (5,000 tasks, a gap of 2,500) with a ~150 ms limit (scaled to your device's speed): brute force won't pass.
Examples
shortest_schedule("AAABBB", 2)→8shortest_schedule("AAABBB", 0)→6shortest_schedule("AAAAAABCDEFG", 2)→16