mediumHeap / Priority QueueFacebookAmazonGoogle
CPU Cooldown Scheduler
A single-core CPU dispatches tasks of various types; each type must wait N cycles before running again. Minimize total cycles (including idles).
Given a list of CPU tasks (letters) and a non-negative integer n representing cooldown between identical tasks, return the least number of time units the CPU takes to finish all tasks.
Choose approach
A
0tick
A
1A
2B
3B
4B
5MapMax-Heap by remaining count
B→3
1/8
Narrationstep 1 of 8
Tick 1: Run A. Cooldown queue size = 1.
Variables
time=1
scheduled=A
heap=B:3
cooldown=A@3
How to think about it
- Running the most-frequent task first minimizes idles.
- Cooldown queue stores tasks with their earliest re-admit tick.
- Idle cycle = heap empty but queue has future-ready tasks.
Edge cases to catch in interview
- n = 0 -> no cooldown; answer = len(tasks).
- All tasks same letter -> many idles.
Examples
Example 1
8{"tasks":["A","A","A","B","B","B"],"n":2,"values":["A","A","A","B","B","B"]}
A B idle A B idle A B
Example 2
6{"tasks":["A","C","A","B","D","B"],"n":1,"values":["A","C","A","B","D","B"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed