Greedy: Greedy choices and reachability bounds
Goal: Learn the pattern first, then apply it to the 8 roadmap problems in this chapter.
When this pattern appears
A locally safe choice can be proved to preserve an optimal solution or extend a frontier.
The reusable idea
Identify the invariant (farthest reach, earliest finish, remaining fuel) and prove the exchange/reset step.
A deliberate problem-solving routine
- Write the input and output in plain language. Identify whether order, duplicates, or mutation matter.
- Work a tiny example by hand; label the state you wish you had at each step.
- Choose the structure or invariant that stores that state.
- Establish a brute-force baseline, then show exactly which repeated work the pattern removes.
- State the invariant before the loop, after each iteration, and at termination.
- Test smallest, boundary, duplicate, and adversarial cases.
Watch the animated pattern
The diagram above this lesson animates the core state changes for this chapter. Use pause and step to predict what moves next. The same invariant should explain each application, even when the problem story changes.
Questions to ask yourself
Why can this local choice not make the global optimum worse?
Common failure modes
A plausible but unproven heuristic, boundary cases, and confusing feasibility with minimization.
Chapter problems from the workbook
- #124 · Maximum Subarray — Day 74, Medium. Open problem ↗
- #125 · Jump Game — Day 74, Medium. Open problem ↗
- #126 · Jump Game II — Day 75, Medium. Open problem ↗
- #127 · Gas Station — Day 75, Medium. Open problem ↗
- #128 · Hand of Straights — Day 76, Medium. Open problem ↗
- #129 · Merge Triplets to Form Target Triplet — Day 76, Medium. Open problem ↗
- #130 · Partition Labels — Day 78, Medium. Open problem ↗
- #131 · Valid Parenthesis String — Day 78, Medium. Open problem ↗
Interview explanation
A concise explanation has four parts: the key observation, the maintained invariant, why each step preserves it, and the time/space complexity. Avoid naming a pattern as a substitute for proving the step is safe.