The Greedy Idea & When It Fails

A meeting room, one day, twenty booking requests: which ones do you accept to fit the most meetings? A rule that fits in one line answers it perfectly, and a nearly identical rule for coins answers wrong. Knowing which is which is the whole skill.

intermediate⏱ 12 min read
01

Learn

The idea, the mechanics and the cost.

01The greedy idea

A greedy algorithm builds the answer one step at a time, and at every step takes the option that looks best right now. It never reconsiders: a choice once made is final.

That makes greedy algorithms short and fast, usually a sort plus one pass, O(n log n). The catch is that "best right now" is not always "best overall". A greedy algorithm is correct only when two things hold:

  • Greedy-choice property: some optimal answer starts with the greedy choice.
  • Optimal substructure: after that choice, what remains is a smaller copy of the same problem.

So greedy is never "obviously right". You pick a rule, then you either prove it or break it with a counterexample.

Every step: take the biggest nowstep 1753step 2492step 3618no going backone pass, no backtracking

02Activity selection: sort by finish time

You have activities [start, end) and one room. Choose as many as possible with no two overlapping.

The rule that works: sort by finish time, then walk the list and take every activity that starts at or after the finish of the last one you took. Intuition: the activity that ends first leaves the most room for everything else.

On the eight activities below this keeps [1,4], [5,7], [8,11]: three meetings, and no schedule fits four. The whole algorithm is a sort plus one scan with a single variable, lastEnd.

Sorted by finish time01234567891011[1,4][3,5][0,6][5,7][3,9][5,9][6,10][8,11]takenskipped (overlap)
Dashed lines: the finish time of the last activity taken.

03Why it is right: the exchange argument

Take any optimal schedule and look at its first activity o₁. Greedy's first pick g finishes earliest of all, so end(g) ≤ end(o₁). Replace o₁ with g: nothing that came after o₁ can overlap g, the count stays the same, so the new schedule is still optimal and now starts with the greedy choice.

Repeat the argument on the rest of the timeline and you transform the optimal schedule into the greedy one, step by step, without ever losing an activity.

This exchange argument is the standard way to prove a greedy rule: show you can swap the greedy choice into an optimal solution without making it worse. Other "natural" rules fail it: earliest start loses to one long activity, shortest first loses to a short one that overlaps two others.

greedy's firstgany optimal answero₁o₂o₃after the swapgo₂o₃g ends no later → no room lostsame count, still no overlap ✓

04When greedy fails: coins {1, 3, 4}

Making change with the fewest coins, the obvious greedy is largest coin first. With real money (1, 2, 5, 10, …) it is always optimal. Now take coins {1, 3, 4} and amount 6:

  • greedy takes 4, then 1, then 1: 3 coins;
  • but 3 + 3 makes 6 with 2 coins.

Grabbing the 4 felt like progress, yet it left a remainder (2) that only small coins can pay. The greedy-choice property simply does not hold for this coin system, and no proof could save it.

The lesson: try small counterexamples before trusting a greedy rule. When greedy fails, dynamic programming (stage 10) considers every option for every smaller amount and finds the true optimum. This exact instance returns there.

Coins {1, 3, 4}, amount 6Greedy: largest first4+1+13 coins ✗Optimal3+32 coins ✓

Cost at a glance

Activity selection (sort + scan)O(n log n)
Scan after sortingO(n)
Greedy change, k coin typesO(k + coins used)

Remember

  1. Greedy takes the locally best option at each step and never undoes it, so it is fast but not automatically correct.
  2. For activity selection, sorting by finish time is provably optimal, and the exchange argument is how you prove it.
  3. One counterexample, like coins {1,3,4} with amount 6, is enough to kill a greedy rule; then reach for DP.
02

Play

Step through the algorithm, then try it on your own input.

👀 What to watch: Watch the dashed line (last finish time): an activity is taken only if it starts at or after it. Then see greedy lose on coins.

Interactive visualizerfocus here, then space ← →
✎ Your inputUp to 10 activities (times 0–24). Try coins 6 4 1 with amount 8, or 25 10 5 1 where greedy is always right.

Activity selection (sorted by finish time)

0246810[1,4][3,5][0,6][5,7][3,9][5,9][6,10][8,11]
Greedy makes the locally best choice and never looks back. Activity selection: pick as many activities as possible that do not overlap. The key move: sort by FINISH time.

Pseudocode

 1 activity selection: sort by finish time 2   take an activity if it starts after the last taken finishes 3   (greedy choice is provably optimal here) 4 coin change greedy: repeatedly take the largest coin ≤ remainder 5   … not always optimal (e.g. coins {1,3,4}, amount 6) 6 compare with the true optimum (found by DP)
1 / 1
03

Check

Three questions. Pick an answer to see why.

Q1

Activities [1,10], [2,3], [4,5], [6,7]. How many does the "earliest finish first" greedy select?

Q2

Coins {1, 5, 6, 9}, amount 11. What does "largest coin first" return?

Q3

What does an exchange argument show?

04

Practice

Real problems to lock it in, easiest first.