2Brute2Best
🔥0

Task Scheduler

Medium·LeetCode #621 Pro OnlySlight Variation
LeetCode
01

Problem Statement

A CPU has a list of tasks. Each task is one letter from A to Z. In each time unit the CPU either runs one task or sits idle. The same task can't run again until at least n units have passed since its last run.

Return the least number of time units needed to finish every task.

Input

An array tasks of uppercase letters, and an integer n, the cooldown.

Output

One integer: the minimum number of time units to run all the tasks.

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

A → B → idle → A → B → idle → A → B. Each letter waits two units before it runs again.

Example 2
Input: tasks = ["A","C","A","B","D","B"], n = 1
Output: 6

A → B → C → D → A → B. A cooldown of 1 is already met by the other tasks.

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

With no cooldown, the tasks just run back to back.

Constraints
  • • 1 ≤ tasks.length ≤ 10^4
  • • Each task is an uppercase English letter.
  • • 0 ≤ n ≤ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“the same task must wait n units before it runs again”
→ Each copy of the top task takes a frame of n + 1 units, so it sets the length of the schedule.
“least number of time units”
→ Idle units are the cost. Count where they are forced, and the answer follows.
“tasks with the same top count”
→ Tasks that tie for the most copies all end in the last row, so each one adds a unit.
03

Approach Complexity Ladder

Brute Force — scan all 26 tasks at every time unitO(T · 26)O(1)

Each time unit, scan all 26 letters for the most frequent one that is not cooling down; idle if none is ready

Better — max-heap of ready tasks and a cooldown queueO(T log 26)O(1)

Ready tasks in a max-heap; a task that just ran waits in a queue until its cooldown ends, then returns to the heap

Optimal — count the frames: (maxF − 1) × (n + 1) + countMaxO(N)O(1)

Only the top count and how many tasks share it matter; the answer is that frame length, but never less than the number of tasks

Brute Force Intuition

Simulate the CPU one unit at a time. At each unit, check all 26 letters and run the one with the most copies left among those that are ready. If none is ready, the unit is idle. Stop when no copies are left.

Recognition rule for this sub-pattern:

The best choice changes as you go — keep candidates in a heap and always pop the current best.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.