Greedy Algorithms
Lesson 18 of 20Watch a meeting room fill by earliest finish, then count out 63 in coins — one irrevocable greedy choice at a time.
The greedy contract
A greedy algorithm makes the best-looking choice right now, commits to it, and never looks back. When the rule is right, that single sweep beats any exhaustive search.
The contract
Take the locally best option, commit irrevocably, move on. No undo, no dp table — usually just a sort followed by one O(n) pass.
Earliest finish wins
Activity selection sorts meetings by end time and books every one that fits: A(1-3), C(4-7) and F(8-10) fill the room — 3 of the 6 requests.
Why it is safe
The meeting that ends earliest leaves the most room behind it. The exchange argument shows swapping it into any schedule never loses a meeting.
Coins, biggest first
Change 63 from {25, 10, 5, 1}: 25 + 25 + 10 + 1 + 1 + 1 — 6 coins. On this canonical set greedy is provably optimal.
When greedy fails
Coins {4, 3, 1}, amount 6: greedy pays 4 + 1 + 1 (3 coins) while 3 + 3 needs just 2. Non-canonical systems break the rule.
Picking meetings
Six meeting requests, sorted by finish time. Booked meetings glow green; turned-away ones fade.
Six meeting requests, already sorted by finish time. Rule: book any meeting that starts at or after the last booked one ends.
The greedy template
Every greedy algorithm is this one loop: sort, test, commit, move on — no revisiting.
Walkthrough
The meeting picker and the change maker in real Java — one sweep each, O(n) after sorting.
1public class Meetings {2public static void main(String[] args) {3String[] name = {"A", "B", "C", "D", "E", "F"};4int[] start = {1, 2, 4, 1, 5, 8};5int[] finish = {3, 5, 7, 8, 9, 10};6int n = 6, count = 0, lastEnd = 0;78for (int i = 0; i < n; i++) {9if (start[i] >= lastEnd) {10count++;11lastEnd = finish[i];12System.out.println("take " + name[i]);13}14}15System.out.println(count + " meetings fit");16}17}
finish = [3, 5, 7, 8, 9, 10] — already sorted ascending, with start aligned to match. That sort is the greedy precondition.
Coin change greedy
Amount 63 from coins {25, 10, 5, 1} — the biggest coin that fits, every single time.
Amount 63 to change, coin set {25, 10, 5, 1}. Greedy rule: always grab the biggest coin that still fits.
Predict the output
A non-canonical register — trace what greedy hands back for 6.
1public class Predict {2public static void main(String[] args) {3int[] coins = {4, 3, 1};4int amount = 6, count = 0, i = 0;56while (amount > 0) {7if (coins[i] <= amount) {8amount -= coins[i];9count++;10} else {11i++;12}13}14System.out.println(count + " coins");15}16}
Hint: Greedy grabs 4 first (6 → 2), then can only pay with 1s (2 → 1 → 0). The optimal 3 + 3 was one wrong first pick away.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Greedy makes the locally best choice and never revisits it — irrevocable commitment.
Activity selection: sort by finish time, take whatever starts at or after lastEnd — O(n log n) total.
The exchange argument proves it: swapping any first pick for the greedy pick never loses ground.
Coin change greedy is optimal only on canonical systems — {25, 10, 5, 1} yes, {4, 3, 1} no.
Sort first, sweep second: nearly every greedy algorithm is a sort plus one O(n) pass.
When no safe greedy rule exists, reach for DP — it revisits so greedy does not have to.