Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get a list tasks of uppercase letters. Each letter is a kind of task, and each task takes exactly one time slot. You also get a cooldown n: between two runs of the same letter there must be at least n other slots (other tasks, or idle slots where the CPU does nothing). You can run the tasks in any order.
Return the minimum number of slots needed to finish everything, idle slots included. There are up to 10⁴ tasks and 0 ≤ n ≤ 100.
Example: tasks = [A, A, A, B, B, B], n = 2 → 8:
A B idle A B idle A BEach A is 3 slots after the previous one, so there are 2 slots in between. With n = 0, the answer would just be 6.
Start with brute force
Trying every order of the tasks is hopeless: 10⁴ tasks have 10⁴! orders.
The first real solution is a simulation. Go one slot at a time. In each slot, run the task with the most copies left among those not cooling down, or idle if every task is cooling down. The busiest task is the one that's hardest to fit, so it should never wait if it doesn't have to. A max-heap holds the counts, and a queue holds tasks that are cooling down along with the slot when they become ready again:
def leastInterval(tasks, n):
heap = [-c for c in Counter(tasks).values()] # max-heap of remaining counts
heapq.heapify(heap)
cooling = deque() # (remaining count, ready-at slot)
time = 0
while heap or cooling:
time += 1
if heap:
left = heapq.heappop(heap) + 1 # run one copy (counts are negative)
if left < 0:
cooling.append((left, time + n))
if cooling and cooling[0][1] == time:
heapq.heappush(heap, cooling.popleft()[0])
return timeThis is correct and it's what many people write in interviews. It costs O(answer · log 26). That's fine here, but it spends one loop per slot, idle slots included. Looking at the shape of its output leads to a one-line formula.
Building the intuition
Run the simulation on AAABBBCCD with n = 2 and write the schedule in rows of width n + 1 = 3:
A B C
A B C
A B DNow try AAAAB, n = 2:
A B idle
A idle idle
A idle idle
AThe busiest task, A, sets up a frame. If it appears f times, it needs f rows. Each row except the last is n + 1 slots wide: one A, then n slots for others. The last row holds only the tasks that appear f times too; they have no cooldown to wait out after that.
So with m tasks tied for the top count, the frame has
(f − 1) × (n + 1) + m slotsEvery other task fits into the gaps. Fill the gaps column by column, taking the other tasks in order of count. A task with fewer than f copies never ends up closer than n + 1 slots to its own next copy. Any gap nobody fills is an idle slot. That's why the frame size is the answer.
What if there are more tasks than gaps? Then you just widen the rows. Each row gets longer than n + 1, the cooldown is more than satisfied, and nobody ever idles. The answer is then simply len(tasks).
Both cases in one line: max(len(tasks), (f − 1) × (n + 1) + m).
This is a greedy argument: the busiest task is the bottleneck. In any schedule, each of the m tied tasks has its last copy at slot (f − 1)(n + 1) + 1 or later, and those m last copies need m different slots. And no schedule can be shorter than the number of tasks. We hit the larger of those lower bounds exactly, so nothing can beat us.
The optimal approach
- Count each letter in a 26-slot array.
f= the largest count.m= how many letters have countf.frame = (f − 1) × (n + 1) + m.- Return
max(frame, len(tasks)).
The subtle detail is m, the tie count. With AAABBB, n = 2, a frame of (3 − 1) × 3 + 1 = 7 is wrong. B also appears 3 times, so the last row is A B, not A, and the answer is 8.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
count = [0] * 26
for t in tasks:
count[ord(t) - ord("A")] += 1
max_count = max(count) # the busiest task sets the skeleton
num_max = sum(1 for c in count if c == max_count) # tasks tied for busiest share the last row
# (max_count - 1) full rows of width n + 1, then one last row holding the tied tasks.
frame = (max_count - 1) * (n + 1) + num_max
# If there are more tasks than slots, no idling is ever needed.
return max(frame, len(tasks))class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int count[26] = {0};
for (char t : tasks) count[t - 'A']++;
int maxCount = *max_element(count, count + 26); // the busiest task sets the skeleton
int numMax = 0; // tasks tied for busiest share the last row
for (int c : count) {
if (c == maxCount) numMax++;
}
// (maxCount - 1) full rows of width n + 1, then one last row holding the tied tasks.
int frame = (maxCount - 1) * (n + 1) + numMax;
// If there are more tasks than slots, no idling is ever needed.
return max(frame, (int)tasks.size());
}
};class Solution {
public int leastInterval(char[] tasks, int n) {
int[] count = new int[26];
for (char t : tasks) count[t - 'A']++;
int maxCount = 0; // the busiest task sets the skeleton
for (int c : count) maxCount = Math.max(maxCount, c);
int numMax = 0; // tasks tied for busiest share the last row
for (int c : count) {
if (c == maxCount) numMax++;
}
// (maxCount - 1) full rows of width n + 1, then one last row holding the tied tasks.
int frame = (maxCount - 1) * (n + 1) + numMax;
// If there are more tasks than slots, no idling is ever needed.
return Math.max(frame, tasks.length);
}
}/**
* @param {character[]} tasks
* @param {number} n
* @return {number}
*/
var leastInterval = function (tasks, n) {
const count = new Array(26).fill(0);
for (const t of tasks) count[t.charCodeAt(0) - 65]++;
const maxCount = Math.max(...count); // the busiest task sets the skeleton
const numMax = count.filter((c) => c === maxCount).length; // tasks tied for busiest share the last row
// (maxCount - 1) full rows of width n + 1, then one last row holding the tied tasks.
const frame = (maxCount - 1) * (n + 1) + numMax;
// If there are more tasks than slots, no idling is ever needed.
return Math.max(frame, tasks.length);
};Dry run
| tasks | n | f | m | frame = (f − 1)(n + 1) + m | len | Answer |
|---|---|---|---|---|---|---|
AAABBB | 2 | 3 | 2 | 2 × 3 + 2 = 8 | 6 | 8 |
AAABBB | 3 | 3 | 2 | 2 × 4 + 2 = 10 | 6 | 10 |
AAABBBCCD | 2 | 3 | 2 | 2 × 3 + 2 = 8 | 9 | 9 (rows widen, no idle) |
AAAAAABCDEFG | 2 | 6 | 1 | 5 × 3 + 1 = 16 | 12 | 16 |
The last one in rows of 3: A B C / A D E / A F G / A _ _ / A _ _ / A → 16 slots.
Complexity
- Time: O(m) for
mtasks. One pass to count, then a scan of 26 counters. - Space: O(1). The count array always has 26 slots.
Edge cases and traps
- Forgetting the tie count.
AAABBBmust give 8. Using+ 1instead of+ mgives 7. - Forgetting
max(..., len(tasks)). Withn = 0, or with many distinct tasks, the frame is smaller than the number of tasks.ABCDEF,n = 3→ frame 1, answer 6. - Counting idle after the last row. The final row has no trailing gaps: that's why it's
(f − 1)full rows plusm, notffull rows. - Thinking the cooldown is
n + 1.nslots must sit between two copies, so copies aren + 1apart. - Char arithmetic.
t - 'A'in C++ and Java;ord(t) - ord("A")in Python;t.charCodeAt(0) - 65in JavaScript.
Say it in the interview
"The most frequent task is the bottleneck. If it appears f times, it needs f − 1 full rows of width n + 1, plus a last row with every task that's tied at count f. That's (f − 1)(n + 1) + m slots, and every other task fits into the gaps without breaking its cooldown. If there are more tasks than gaps, I widen the rows and nobody idles, so the answer is just the task count. So I return the max of the two. It's O(n) to count and O(1) space. I can also show the heap simulation, which is how I'd generate the actual schedule."
Follow-ups interviewers ask
- "Return the actual schedule, not just its length." Use the heap simulation from the brute-force section and record what runs in each slot.
- "What if tasks must run in the given order?" Then there's no choice to make. Walk the list, keep a hash map of the earliest slot each letter may run next, and jump time forward when needed.
- "Rearrange a string so no two equal letters are adjacent." That's this problem with
n = 1. It's possible only iff ≤ (len + 1) / 2, and the "most remaining first" heap builds it.
Practise the same idea
This is Day 64 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free