Foundations · Unit 14
Greedy
A greedy algorithm makes the choice that looks best right now and never reconsiders. To make change, grab the largest coin that fits, repeat. To schedule the most meetings, always take the one that finishes earliest. Greedy is fast, simple, and uses almost no memory, which is exactly why it is tempting.
The catch, and the entire reason this unit exists, is that the locally-best choice is not always globally best. Sometimes grabbing the biggest coin now forces a worse total later. When greedy works, it is the cleanest solution there is. When it does not, it quietly returns a wrong answer that looks plausible. The skill is not writing greedy code, that part is easy. The skill is knowing whether greedy is safe before you trust it.
This unit builds that judgment: what greedy is, the kind of reasoning that proves it is safe, the way it fails, and the one question that separates a greedy problem from a dynamic-programming one. Get this distinction and you will stop guessing on a whole class of optimization problems.
You’re not signed in. Your progress here won’t be saved.
The idea: take the best choice now, never look back
Greedy means: at each step, make the choice that looks best by some simple rule, commit to it, and move on. No backtracking, no exploring alternatives.
Scheduling is the classic win. To fit the most non-overlapping meetings in a day, repeatedly pick the meeting that finishes earliest among those that still fit. Each choice frees up the most remaining time, and it turns out this always yields the maximum number of meetings. One pass, no second-guessing.
What does a greedy algorithm do at each step?
The drills and graded test are premium
The lessons above are free. The interactive practice that turns recognizing the pattern into recalling it on a blank page unlocks with premium:
- ✓3 applied drills: train spotting when the concept applies
- ✓A 5-question graded test to clear the unit
- ✓The full guided problem ladder with faded hints, plus the cold-read capstone
From $6/month. Cancel anytime.