All topics
105

Paying out with the fewest coins

Tier 1Locked

Where it runs

1. The problem

Many scheduling and graph problems are solved like this: at each step, take the option that looks best right now. That is the greedy approach. It always needs a reason: why the choice that is best at one step does not spoil the whole answer.

A cashier in an American shop owes you 40 cents in change. The coins there are 25, 10, 5 and 1 cent. The cashier does not count out forty one-cent coins. They take a 25, then a 10, then a 5, and they are done: three coins. Nobody taught them an algorithm. Again and again they take the biggest coin that is not more than what is still owed, until the change is paid.

You want the same thing in code. You have the coin values a machine holds and an amount to pay out. Pay it with the fewest coins. An ATM solves this every time it counts out notes. A vending machine solves it every time it drops your change.

The rule "take the biggest" almost always gives the best answer. In this lesson you learn on which coin sets it works and on which it goes wrong.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included
Paying 18 with the fewest coins, greedily
minCoinsGreedy([1, 5, 2, 10], 18)
coins
1
5
2
10
amount = 18
used
empty

Pay 18 with as few coins as possible. Greedy rule: always take the biggest coin that still fits.

Speed race
Every subset and the greedy pick on the same 11 meetings: the most that fit without overlapping

Bars: when each meeting ends, in the order the meetings start.

One operation is one meeting checked or one comparison while sorting. Every frame, each lane does the same number of operations, so the slow one falls behind.

Every subset O(2ⁿ·n)
0 operations
Greedy by end time O(n log n)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 10
Every subset2.2K · under 1 ms
Greedy by end time33.3 · under 1 ms
n = 20
Every subset2.2M · 22 ms
Greedy by end time80.5 · under 1 ms
n = 40
Every subset2.3T · 6.4 h
Greedy by end time189 · under 1 ms

Recognise it

Greedy (exchange argument)

Say these words and it is this pattern

  • fewest coins or pieces to make an amount
  • largest that still fits, taken greedily
  • make change with denominations
  • prove optimality with an exchange argument

Looks similar, isn't

O(n log n) when the input has to be sorted first. The pass afterwards is a single loop, O(n).

On LeetCode

Problems where one simple rule, such as the biggest first or the smallest first, gives the best answer. Before you trust the rule, run the exchange argument and show that swapping its choice for any other never makes the answer better. Coin Change itself fails this check and comes in lesson 112.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy