Search lands in PR-5.1 (Pagefind).

How-to Intermediate

Chapter 2 Updated

Task Scheduler

Frame the most frequent task; fill gaps. Closed-form: (max_f − 1) × (n + 1) + ties.

  • Full 5m
  • Revision
  • Flow

Problem Statement

Given an array of CPU tasks (identified by uppercase letters) and an integer n — the cooldown between two identical tasks — return the minimum number of time units needed to finish every task. Each unit either runs a task or is idle.

Input

  • tasks — a list of character task identifiers.
  • n — the non-negative cooldown between identical tasks.
Example input
tasks = ["A","A","A","B","B","B"], n = 2

Output

A single integer — minimum time units needed.

Expected output
8

Constraints

  • 1 ≤ tasks.length ≤ 10⁴
  • tasks[i] is an uppercase English letter.
  • 0 ≤ n ≤ 100

Intuition

The most-frequent task dominates the schedule. Call its frequency max_f. Line up the max_f copies with n cooldown slots between them:

Frame of the most frequent task
A _ _ _ A _ _ _ A          ← max_f = 3, n = 3
└─(n+1)─┘└─(n+1)┘└A┘

That gives (max_f − 1) × (n + 1) + 1 slots. If multiple tasks tie at max_f, each takes an extra trailing slot — add max_count − 1 more. If there are enough other tasks to fill all idle gaps, the answer is simply tasks.length (no idle time needed).

Answer: max( tasks.length, (max_f − 1) × (n + 1) + max_count ).

Walkthrough — tasks AAABBB, n = 2
max_f = 3 (A and B)        max_count = 2
Frame: A _ _ A _ _ A   → length (3−1)×(2+1) + 2 = 8
Schedule:  A B _ A B _ A B  → 8 units

Solution

task_scheduler.py
from collections import Counter
 
def leastInterval(tasks: list[str], n: int) -> int:
    freq = Counter(tasks)
    max_f = max(freq.values())
    max_count = sum(1 for v in freq.values() if v == max_f)
 
    return max(len(tasks), (max_f - 1) * (n + 1) + max_count)

Complexity

MetricValueReason
TimeO(n)Single counting pass over tasks; alphabet is at most 26
SpaceO(1)Bounded alphabet — at most 26 frequency entries

Edge Case Thinking

ScenarioHandling
n = 0Cooldown is zero → formula gives (max_f − 1) × 1 + max_count ≤ tasks.length, so tasks.length wins.
All tasks uniquemax_f = 1 → formula gives 0 + max_count; tasks.length wins.
Enough other tasks to fill gapstasks.length exceeds the framed value → no idle time needed.
Dominant single task — ["A","A","A"], n = 2Formula gives (3−1)·3 + 1 = 7 time units (with idles).
Ties at the top — AAABBBBoth A and B at max frequency; the + max_count term places the last B and last A side by side.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.