A greedy algorithm takes the locally best choice at every step and never revisits it — blazing fast when that choice is provably safe.
1 The greedy choice
Grab whatever looks best right now — the biggest coin, the earliest-finishing task — and lock it in forever.
2 Coin change
63¢ from {25, 10, 5, 1} takes six coins: 25 + 25 + 10 + 1 + 1 + 1 — found by always taking the biggest that fits.
3 Activity selection
Sort by finish time, keep every activity with start ≥ lastEnd — three of these five activities survive.
4 Proof by exchange
Swap any optimal solution toward the greedy one, piece by piece — if the value never drops, greedy is optimal.
5 The trap
Coins {1, 3, 4} and amount 6: greedy pays 4+1+1 = 3 coins, but 3+3 = 2. Fast is not always right.
Why greedy works
The greedy choice1Grab whatever looks best right now — the biggest coin, the earliest-finishing task — and lock it in forever. Coin change263¢ from {25, 10, 5, 1} takes six coins: 25 + 25 + 10 + 1 + 1 + 1 — found by always taking the biggest that fits. Activity selection3Sort by finish time, keep every activity with start ≥ lastEnd — three of these five activities survive. Proof by exchange4Swap any optimal solution toward the greedy one, piece by piece — if the value never drops, greedy is optimal. The trap5Coins {1, 3, 4} and amount 6: greedy pays 4+1+1 = 3 coins, but 3+3 = 2. Fast is not always right. 5 building blocks make up Why greedy works
Amount 63, coins {25, 10, 5, 1}. The biggest coin that fits gets taken — every single time.
1 / 11Make change for 63 with coins {25, 10, 5, 1}. Greedy rule: take the biggest coin that still fits, as often as it fits.
Activity selection in eight boxes: sorted order in, greedy picks out.
process decision input / output start / end The greedy cashier counting coins, then the scheduler filling a day — both in plain C++.
mainFour denominations, biggest first — the greedy order matters.
Memory
coins= {25, 10, 5, 1}
Console
output appears as the program runs…
Five meetings, one room. Sorted by finish time, the earliest finisher always goes first.
1 / 11Five activities sorted by finish time. lastEnd = -1 — any start beats that, so the first one always fits.
Same greedy cashier, hostile coins — trace it and see what it costs.
Your predicted output — one line per console line
Hint: Greedy grabs the 4 first: 6 − 4 = 2, then two 1s. The optimal answer is 3 + 3 — just two coins. With awkward denominations, greedy loses.
Click a term, then click its meaning to lock the pair.
7 conceptsTap a card to expand ?greedy choicedrops into place
?earliest finishdrops into place
?start ≥ lastEnddrops into place
?exchange argumentdrops into place
?sort firstdrops into place
?no backtrackingdrops into place
01Greedy = best local choice, locked in forever — no undo, no backtracking.
02Coin change is greedy-safe for {25, 10, 5, 1} — 63¢ takes just 6 coins.
03Activity selection: sort by finish time, pick anything with start ≥ lastEnd.
04The exchange argument is how you prove a greedy strategy is truly optimal.
05Coins {1, 3, 4} trap greedy: amount 6 costs 3 coins instead of 2.
06When the greedy-choice property fails, hand the problem to dynamic programming.