Skip to content
AI360Xpert
Beta
Difficulty: MediumHeap / Priority Queue

Task Scheduler

Problem in Plain English

Given tasks labeled A through Z and a cooldown n forcing identical tasks at least n intervals apart, arrange every task into the shortest schedule that may include idle slots. Return that minimum total interval count.

Problem Statement

Given a list of tasks tasks where each entry is an uppercase letter, and a cooldown integer n, schedule every task on a CPU that handles one task or one idle slot per interval.

Between consecutive executions of the same letter, at least n complete intervals must pass, while different tasks may run back to back in any order.

Return the smallest possible total number of intervals, counting both work and idle time.

Constraints

  • 1 <= tasks.length <= 10^4
  • tasks[i] is an uppercase English letter.
  • 0 <= n <= 100

Examples

Example 1

Input
tasks = ["A","A","A","B","B","B"], n = 2
Output
8

One best order is A -> B -> idle -> A -> B -> idle -> A -> B, which keeps two intervals between equal letters.

Example 2

Input
tasks = ["A","A","A","B","B","B"], n = 0
Output
6

With no cooldown, any order works, for example A -> A -> A -> B -> B -> B, so no idle slot is needed.

Example 3

Input
tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
Output
16

One best order is A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A.

Intuition

Repeated letters, rather than task order, force the idle time. In Example 1, three copies of A need two gaps of two intervals; B fills one interval in each gap but cannot fill both because B also has a cooldown. A heap can simulate the schedule one interval at a time. Counting the largest frequency and how many letters share it gives the same minimum without constructing the schedule.

Approaches

Cooldown Simulation

Solution Details

Reveal Cooldown Simulation: intuition, complexity, and code

Hints

Hint 1
The most frequent task sets the shape of the whole schedule, so start by counting frequencies and naming the maximum.
Hint 2
Picture the top task splitting time into maxFreq - 1 blocks of n + 1 slots each, with the other top tasks filling the tail.
Hint 3
Idle slots only matter when the block frame outgrows the task list, so the answer is the larger of the frame size and tasks.length.

Edge Cases

  • n = 0 removes every cooldown, so any order works and the answer is exactly tasks.length.
  • Enough distinct tasks to fill every gap, like many letters with n = 2, also needs zero idle time.
  • A single repeated task, such as six copies of A with n = 2, forces maximum idle: five width-3 gaps plus a final A give 16 intervals.
  • One task returns 1 even with n = 100; never add a cooldown after the final task.
  • More than n + 1 letters tied for maximum frequency makes the frame smaller than the actual task count; return T.

Common Mistakes and Interview Tips

  • Returning the frame size without comparing against tasks.length, which undercounts when the cooldown is tiny.
  • Counting only one task at the top frequency and forgetting maxCount, which drops the tail slots of tied tasks.
  • In the simulation, releasing cooled tasks before running the current slot, which lets a task run one interval too early.
  • Using O(T * n) as the simulation bound gives zero work when n = 0; use O(T(n + 1)).
  • Treating each queued execution as a permanent entry overstates peak space: a letter is in exactly one structure, and the alphabet has only 26 letters.
  • Interview tip: explain both lower bounds, T and (f - 1)(n + 1) + k, before applying the formula.

Key Takeaway

Cooldown schedules are won by serving the highest remaining frequency first; when only the shape of the answer matters, the most frequent item often yields a closed-form frame.