Greedy Algorithms
Lesson 18 of 20Sort meetings by finish time and book them greedily — then watch coins {4, 3, 1} trip the strategy that 25/10/5/1 never trips.
Why greedy works
One pass, one sort, zero regret: greedy bets that the locally best choice never needs undoing — and usually the bet pays.
The greedy deal
Take the locally best move now and never reconsider. One pass, tiny state, instant answers — no table, no recursion.
The sort is the strategy
Activity selection sorts by finish time: the earliest finisher leaves the most room behind it — 6 bookings, one comparison each.
One variable of state
The whole scan needs last_end — start ≥ last_end means compatible, and back-to-back (4 ≥ 3) counts as a fit.
Greedy works… usually
Canonical coins like {25, 10, 5, 1} split any amount optimally biggest-first: 87 needs exactly 25+25+25+10+1+1.
…and how it fails
Coins {4, 3, 1}, amount 6: greedy takes 4+1+1 (3 coins); optimal is 3+3 (2). No sort key can save it.
The proof move
Exchange argument: swap any optimal pick for the greedy one — the solution stays valid, so greedy was safe all along.
Book the room
Six bookings, one room, sorted by finish time. Green = booked, faded = skipped for good.
Six bookings for one meeting room, pre-sorted by finish time. Rule: take any act whose start is ≥ the last booked end. lastEnd = 0.
Greedy loop
The entire scheduler: one sort, one comparison, no second guesses.
Two greedy scans
The scheduler filling a day, then the cashier breaking 87 — both one pass, both zero regret.
1acts = [(1, 3), (2, 5), (4, 7), (6, 8), (8, 10), (9, 11)]2acts.sort(key=lambda a: a[1])34picked = []5last_end = 067for start, end in acts:8if start >= last_end:9picked.append((start, end))10last_end = end1112print(len(picked), 'activities:', picked)
Six bookings as (start, end) tuples — the day’s calendar.
When coins betray
Same greedy rule, hostile coins: {4, 3, 1} for amount 6. The biggest-first pick blocks the perfect answer.
Make change for 6 from coins {4, 3, 1}, biggest first. Greedy rule: take the biggest coin that fits. Spoiler: this system trips greedy.
Predict the output
The greedy cashier meets hostile coins — count the picks.
1coins = [4, 3, 1]2amount = 63used = []4for coin in coins:5while amount >= coin:6amount -= coin7used.append(coin)8print(len(used), used)9print('sum:', sum(used))
Hint: Greedy grabs the 4 before the 3s ever get a look — the optimal 3 + 3 is unreachable. Two 1s mop up the remainder 2.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Greedy = one safe local choice per step, zero backtracking — the sort key IS the strategy.
Activity selection: sort by finish time, keep any act with start ≥ last_end.
The exchange argument proves greedy safe: swapping an optimal pick for the greedy one still works.
Canonical coin systems (25/10/5/1): biggest-first is always optimal.
Coins {4, 3, 1} break greedy: 4+1+1 (3 coins) vs the optimal 3+3 (2 coins).
No provably safe choice? Stop greedy-ing and build a DP table.