Problem Statement

Task Scheduler

You are given an array of tasks, where each item is a single letter standing for a job the CPU has to run (like "A" or "B"). You are also given a number n, the cooldown. The cooldown means that after the CPU runs a task, it must wait n time slots before it can run that same task again. Different tasks have no wait between them. If there is nothing else to run, the CPU sits idle for a slot. Return the smallest number of time slots needed to finish every task.

mediumHeapHeap / Priority QueueTime: O(n log 26) = O(n) · Space: O(1)

Signals to notice

schedule tasks with cooldownmost frequent task limits throughputidle slots

Brute force first

Simulate each time unit, greedily picking the most frequent available task. Correct but slow simulation. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

The key insight

The total time = max(n, (maxFreq - 1) × (cooldown + 1) + countOfMaxFreq). The most frequent task creates mandatory gaps — other tasks fill those gaps. If there are enough diverse tasks, no idle time is needed. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Trace it on tasks=["A","A","A","B","B","B"], n=2

init: count={A:3,B:3}, heap=[-3,-3], queue=[], time=0
t=1: pop A(3->2), queue=[(-2,3)]; queue front ready@3 != 1, heap=[-3]
t=2: pop B(3->2), queue=[(-2,3),(-2,4)]; front ready@3 != 2, heap=[]
t=3: heap empty, no pop (idle); front ready@3 == 3 -> push back, heap=[-2], queue=[(-2,4)]
t=4: pop A(2->1), queue=[(-2,4),(-1,6)]; front ready@4 == 4 -> push back B, heap=[-2], queue=[(-1,6)]
t=5: pop B(2->1), queue=[(-1,6),(-1,7)]; front ready@6 != 5, heap=[]
t=6: heap empty, idle; front ready@6 == 6 -> push back A, heap=[-1], queue=[(-1,7)]
t=7: pop A(1->0, dropped); front ready@7 == 7 -> push back B, heap=[-1]; t=8: pop B(dropped), heap & queue empty -> loop ends, return time=8

What must stay true

The most frequent task determines the minimum timeline. It must wait (cooldown + 1) units between executions, creating (maxFreq - 1) gaps. If other tasks fill all gaps, the answer is just n. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Shape of the loop

heap = max-heap of task frequencies; queue = []; time = 0
while heap or queue:
    time += 1
    if heap: cnt = pop(heap) - 1; if cnt > 0: queue.push((cnt, time + n))
    if queue and queue.front.readyTime == time: heap.push(queue.popleft().cnt)
return time

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Trying to simulate task-by-task — the formula approach is and captures all cases. The key insight is that idle time only exists when there aren't enough diverse tasks to fill the gaps. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Heap / Priority Queue Pattern