Opening scenario

Task Scheduler (LC 621): you're given a list of CPU tasks like ["A","A","A","B","B","B"] and a non-negative cooldown n. Each task takes one time unit. Between two runs of the same task there must be at least n units of gap (any other task or an explicit idle). Return the minimum number of time units to finish everything. For the example with n = 2, one optimal schedule is A B idle A B idle A B → 8.

A second example uses a different greedy rule: Text Justification (LC 68) — preserve word order and greedily pack as many words as fit into each line of exactly maxWidth characters. Spread spaces evenly between words. Assign any remainder to the leftmost gaps. Left-justify the final line and single-word lines.

Assume the task list is non-empty and uses the 26 uppercase English letters. For text justification, words is non-empty, maxWidth is positive, and each non-empty word fits on a line by itself. Compare what forces a delay in the schedule with what forces a line break in the text. LC numbers identify LeetCode problems.

In the full tutorial

  • Question progression
  • Canonical Template
  • Complexity
  • Pattern Recognition Signals
  • Practice Set
  • Pitfalls
  • Variations
  • Closing: When to Reach for This

Glossary history

Click dotted jargon to save explanations here.