Home
DSA Patterns
Greedy Algorithms
Task Scheduler
Task Scheduler (LeetCode 621)
Task Scheduler (LeetCode 621): find the fewest CPU intervals to run every task with n idle gaps between repeats, from a frame set by the most frequent task.
Problem Understanding
Task Scheduler: given tasks labelled A–Z and a cooldown n, every interval the CPU runs one task or idles, and two runs of the same task must be at least n intervals apart. Return the fewest intervals needed to run every task.
Example: tasks = [A, A, A, B, B, B], n = 2 returns 8: A B idle A B idle A B.
Attempt 1: Simulate with a Heap
Run the schedule interval by interval: in each round of n + 1 slots, take the most frequent remaining tasks from a max-heap, run them, and push them back with one fewer. It is correct, but it simulates every interval. Counting alone tells us the answer, because only the most frequent task can force the CPU to idle.
The Intuition: The Most Frequent Task Builds a Frame
Say A is the most frequent task, with maxCount runs. Between two runs of A there must be n other slots, so lay the schedule out in rows of n + 1, one row per run of A:
A ? ?
A ? ?
A
That is maxCount − 1 full rows and a last row. Every task tied with A also needs a run in that last row, so the frame holds (maxCount − 1) · (n + 1) + maxTasks slots.
Fill the other tasks into the gaps column by column. A letter never lands twice in one row, so its runs are at least n + 1 apart. Gaps left over are idle time. If there are more tasks than gaps, the rows simply get longer and nothing idles, so the answer is max(len(tasks), frame). This is a greedy argument: the frame is a lower bound, and the fill shows it can always be met.
Task Scheduler Visualization
6 TASKS, n = 2
COUNT
counts
maxCount
:
Tied
:
Answer
:
Count A: 1
Next
Count A: 2
Tasks
n
The tally fills first. Once the top count is known the frame appears, one row per run of the most frequent task, read left to right as time. Each letter then fills down the columns; dashed cells are idle.
Task Scheduler Dry Run
Input: tasks = [A, A, A, B, B, B], n = 2
| Quantity | Value |
|---|---|
| counts | A = 3, B = 3 |
| maxCount | 3 |
| maxTasks | 2 (A and B) |
| frame | (3 − 1) · (2 + 1) + 2 = 8 |
| answer | max(6, 8) = 8 |
The frame reads A B _ | A B _ | A B: two idle slots.
Task Scheduler LeetCode Solution
Count, find the top count and its ties, and size the frame.
Task Scheduler Code
1var leastInterval = function(tasks, n) {2 const counts = new Array(26).fill(0);3 for (const t of tasks) counts[t.charCodeAt(0) - 65]++;4 const maxCount = Math.max(...counts);5 const maxTasks = counts.filter((c) => c === maxCount).length;6 const frame = (maxCount - 1) * (n + 1) + maxTasks;7 return Math.max(tasks.length, frame);8};
Task Scheduler in Python, Java, C++, TypeScript and C
Task Scheduler in Python
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
counts = Counter(tasks)
max_count = max(counts.values())
max_tasks = sum(1 for c in counts.values() if c == max_count)
frame = (max_count - 1) * (n + 1) + max_tasks
return max(len(tasks), frame)Task Scheduler in Java
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] counts = new int[26];
for (char t : tasks) counts[t - 'A']++;
int maxCount = 0, maxTasks = 0;
for (int c : counts) {
if (c > maxCount) { maxCount = c; maxTasks = 1; }
else if (c == maxCount) maxTasks++;
}
int frame = (maxCount - 1) * (n + 1) + maxTasks;
return Math.max(tasks.length, frame);
}
}Task Scheduler in C++
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
int counts[26] = {0};
for (char t : tasks) counts[t - 'A']++;
int maxCount = *max_element(counts, counts + 26);
int maxTasks = count(counts, counts + 26, maxCount);
int frame = (maxCount - 1) * (n + 1) + maxTasks;
return max((int)tasks.size(), frame);
}
};Task Scheduler in TypeScript
function leastInterval(tasks: string[], n: number): number {
const counts: number[] = new Array(26).fill(0);
for (const t of tasks) counts[t.charCodeAt(0) - 65]++;
const maxCount = Math.max(...counts);
const maxTasks = counts.filter((c) => c === maxCount).length;
const frame = (maxCount - 1) * (n + 1) + maxTasks;
return Math.max(tasks.length, frame);
}Task Scheduler in C
int leastInterval(char* tasks, int tasksSize, int n) {
int counts[26] = {0};
for (int i = 0; i < tasksSize; i++) counts[tasks[i] - 'A']++;
int maxCount = 0, maxTasks = 0;
for (int i = 0; i < 26; i++) {
if (counts[i] > maxCount) { maxCount = counts[i]; maxTasks = 1; }
else if (counts[i] == maxCount) maxTasks++;
}
int frame = (maxCount - 1) * (n + 1) + maxTasks;
return tasksSize > frame ? tasksSize : frame;
}Edge Cases & Common Mistakes
- n = 0: no cooldown; the frame is never larger than the task count, so the answer is
len(tasks). - Ties for the top count: forgetting
maxTasksand adding 1 undercounts the last row:[A, A, B, B], n = 2needs 5, not 4. - More tasks than slots: without
max(len(tasks), frame)the formula returns too small a number, as in[A, C, A, B, D, B], n = 1. - Building the real schedule: the question asks only for the length; you never need to output the order.
Summary
Only the most frequent task can force idle time. It sets a frame of (maxCount − 1) · (n + 1) + maxTasks slots, every other task fits in the gaps column by column, and extra tasks just widen the rows. Return max(len(tasks), frame): O(n) time, O(1) space.
More practice: DSA interview questions with answers · patterns cheat sheet
