Medium · Frequency Max Heap
Understand → Visualise → Practice.
Identical task letters need at least cooldown slots between executions. Return the minimum total slots including idle time.
If the most frequent task occurs f times, it needs (f − 1) gaps of length cooldown + 1, plus a final slot for each task tied for most frequent. Other tasks fill the gaps.
Find the highest frequency f and how many tasks t share it.
frame = (f − 1) × (cooldown + 1) + t.
If there are more tasks than the frame holds, no idle slots are needed: return max(len(tasks), frame).
O(n) time and O(1) space for 26 task letters.
Forgetting the max with len(tasks) undercounts when many different tasks fill every gap.
Read the full guide first: Heaps and Priority Queues.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.