Greedy Algorithms
The Mental Model
Greedy is about making the best choice at each step without looking back. Imagine you’re collecting coins scattered on a path—you pick up the highest value coin at each position. Sometimes this works perfectly; sometimes it fails spectacularly.Pattern Recognition Signals:
- “Minimum number of operations/items”
- “Maximum value/count possible”
- Problem has optimal substructure + greedy choice property
- Sorting + processing often leads to answer
When Greedy Works (and When It Doesn’t)
Greedy Works When
- Local optimal leads to global optimal
- No need to reconsider past choices
- Exchange argument can prove correctness
- Problem has “greedy choice property”
Greedy Fails When
- Current choice affects future options
- Need to try all possibilities (use DP)
- Counter-example exists
- Problem mentions “subsequence” (often DP)
The Greedy Proof: Exchange Argument
To prove greedy works, show that swapping from greedy to non-greedy never improves the answer. This is the most common proof technique for greedy algorithms in competitive programming. The template:- Assume an optimal solution O that differs from the greedy solution G
- Find the first place where they differ
- Show you can “exchange” O’s choice for G’s choice without making things worse
- Repeat until O matches G — proving G is also optimal
Pattern 1: Interval Scheduling
Problem: Select maximum number of non-overlapping intervals. Greedy Strategy: Sort by end time, always pick next non-overlapping.Pattern 2: Task Scheduling with Deadlines
Problem: Tasks have deadlines and durations. Minimize lateness. Greedy Strategy: Sort by deadline (Earliest Deadline First).Pattern 3: Fractional Knapsack
Problem: Items have weight and value. Maximize value in capacity W. Can take fractions. Greedy Strategy: Sort by value/weight ratio (value per unit weight), take as much as possible of the best items first. Why this works: Since we can take fractions, there is no penalty for “partially using” an item. The optimal strategy is to always take the item giving you the most value per kilogram.Pattern 4: Huffman Encoding (Min Cost Merging)
Problem: Merge elements repeatedly, cost = sum of merged elements. Minimize total cost. Greedy Strategy: Always merge the two smallest. Why the two smallest? Every element’s value gets “re-paid” at every merge it participates in. Elements merged later participate in fewer future merges. So putting the largest elements into late merges (few future payments) and smallest into early merges (many future payments) minimizes total cost. Worked Example: arr = [1, 2, 3, 4]- Merge 1 + 2 = 3, cost = 3. Array: [3, 3, 4]
- Merge 3 + 3 = 6, cost = 6. Array: [4, 6]
- Merge 4 + 6 = 10, cost = 10. Array: [10]
- Total cost = 3 + 6 + 10 = 19
Pattern 5: Jump Game
Problem: Can you reach the last index? arr[i] = max jump from i. Greedy Strategy: Track the farthest position reachable. Analogy: Imagine you are walking along a number line. At each position, you see how far ahead you could leap. You do not actually need to jump at every position — you just keep track of the farthest point any position so far could reach. If at any position i you find you could never have gotten there (i > maxReach), the answer is “no.”Pattern 6: Gas Station
Problem: Circular route with gas stations. Find starting point to complete circuit. Greedy Insight: If total gas >= total cost, a solution exists. Start from the point where running sum is minimum. Why this works: If the tank drops below 0 at station i, no station from the currentstart through i can be the answer (they would all run out of gas at or before i). So we reset and try starting from i + 1. If the total gas is sufficient globally, the last reset point is guaranteed to work.
Edge case: If all gas[i] == cost[i], the answer is 0 (any starting point works since the tank never goes negative). If total gas < total cost, no starting point works.
Common Greedy Mistakes
The Greedy Decision Flowchart
Practice Problems
Beginner (800-1100)
Intermediate (1100-1400)
Advanced (1400-1700)
Key Takeaways
Exchange Argument
Prove greedy by showing swaps don’t help.
Sort First
Most greedy solutions start with sorting.
Trust But Verify
Greedy intuition can be wrong. Test edge cases.
Know When to DP
“Subsequence” and “count ways” usually need DP.
Next Up
Chapter 10: Divide & Conquer
Learn to split problems in half, solve each part, and combine results efficiently.