Computer Science I / Greedy Choices and Counterexamples
Practice question · Put in order

Order what happens when greedy makes change for 30 with coins {1, 15, 20}.

Hints
  1. Greedy always grabs the largest coin that fits.
  2. After 20, no coin but 1 fits into the remaining 10.
Show the answer
  1. Take 20, the largest coin not exceeding 30
  2. Now 10 remains; take 1, since 15 and 20 are too big
  3. Keep taking 1s until nothing remains
  4. End with 11 coins, worse than the optimal 2
Why

Greedy grabs 20, then is forced onto ten 1s, ending at 11 coins, while two 15s would have done it in 2. A locally best first move led straight to a globally worse answer.

Read the lesson: Greedy Choices and Counterexamples →

Practise Greedy Choices and Counterexamples

The app has 5 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Greedy Choices and Counterexamples