A greedy algorithm builds its answer one step at a time, and at each step it makes the choice that looks best right now, without ever going back to change it.
That makes greedy algorithms short and fast (often one pass, O(n)). The catch: the "best right now" choice is not always the best overall. The hard part of a greedy problem is knowing why the greedy choice is safe.
When greedy works, and when it doesn't
Works: in Jump Game, you only track the farthest index you can reach. Taking the max at each step never hurts you, because a bigger reach includes every smaller one.
Fails: in Coin Change with coins [1, 3, 4] and amount 6, "take the biggest coin that fits" gives 4 + 1 + 1 (3 coins), but 3 + 3 needs only 2. Taking the 4 looked best, but it left an awkward remainder. That problem needs DP.
| Problem | Greedy choice | Safe? |
|---|---|---|
| Jump Game | keep the farthest reach | ✓ |
| Maximum Subarray | drop the running sum when it's negative | ✓ |
| Coin Change | take the biggest coin that fits | ✗ |
| House Robber | rob every other house | ✗ |
How to tell if a greedy choice is safe
You don't need a formal proof in an interview, but you should be able to give a short argument. Two common ones:
- "It never hurts" (stays ahead): the greedy choice keeps you at least as well off as any other choice. A bigger reach in Jump Game contains every smaller reach.
- "Swap it in" (exchange argument): take any best answer that doesn't make the greedy choice, and show you can swap the greedy choice in without making it worse. In Maximum Subarray, if the best subarray started with a negative-sum prefix, cutting that prefix off would make it bigger, so the best one never starts that way.
And always try to break it: test your greedy rule on small tricky inputs. If you find a counterexample, it's probably a DP problem.
Greedy and DP are related
Many greedy algorithms are a DP where you can prove only one number matters:
- Maximum Subarray: the DP is "best sum ending at
i"; greedy notices it's justmax(x, previous + x). - Jump Game: the DP is "
canReach[i]for everyi" (O(n²)); greedy notices the reachable indexes are always a solid block, so one number (farthest) describes them all (O(n)).
So if you can't see the greedy rule, start with the DP, and look for a way to shrink it.
Common greedy shapes
- Keep a running best and update it in one pass: farthest reach, best sum so far.
- Sort first, then take items in order: by end time, by size, by deadline. (You'll see this in the Intervals category.)
- Always take the smallest/largest available, often with a heap.
How to recognize a greedy problem
- There's an obvious "best local choice", and the problem asks for one optimal value (max, min, yes/no).
- A slower DP solution exists, but each step only seems to need one summary number.
- Sorting the input makes the right choice obvious.
Common mistakes
- Trusting a greedy rule without testing it. Always try a small input designed to fool it.
- Applying greedy to a DP problem like Coin Change with arbitrary coins.
- Starting a "best" variable at
0when every value could be negative.