Task Scheduler

mediumTests passed 2026-11-11v1.0.0

1. Core Invariant / The Trick

Most frequent task (`maxFreq` occurring `maxCount` times) dictates frame geometry: `(maxFreq - 1)` full chunks of length `(n + 1)`, plus a final partial frame of `maxCount` tasks. If non-max tasks fill all idle slots, total time simply equals `len(tasks)`: `max(len(tasks), (maxFreq - 1) * (n + 1) + maxCount)`.

2. Complexity Bounds & Proof

26 uppercase task labels take O(1) auxiliary frequency storage; frequency counting and closed-form evaluation run in one linear pass.

3. Minimal Idiomatic Code

def leastInterval(tasks: list[str], n: int) -> int:
    freq = [0] * 26
    for t in tasks:
        freq[ord(t) - 65] += 1
    max_f = max(freq)
    max_cnt = freq.count(max_f)
    return max(len(tasks), (max_f - 1) * (n + 1) + max_cnt)

4. Failure Modes & Interview Follow-ups

Pitfalls:- Forgetting `max(len(tasks), formula)` undercounts when extra task types overflow the idle frame width `n + 1`. - Heap + cooldown queue simulation is O(T log 26) and useful if asked for an explicit schedule sequence.
Follow-ups:- If asked to emit an actual valid execution order, pair a max-heap with a cooldown FIFO queue `(readyTime, remainingCount, task)`.

Personal Notes & Scratchpad (Local-First Yjs CRDT)

Saved instantaneously in IndexedDB. Streams live across peers via Hocuspocus when signed in.

Related Invariants

Browser Scratchpad
Console Output
Click Run or press Cmd/Ctrl+Enter to execute locally in a Web Worker.