Skip to content

Greedy change-making

planning ahead

Level 2 of 3

Also called Biggest coin first and The cashier's algorithm.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many comparisons does greedy make paying 8 with coins of 6, 4 and 1?

the answer, and why

the answer is D

6 fits in 8, then 6 and 4 do not fit in 2, then two 1s fit and a third does not. 3 coins taken and 3 kinds.

if you chose another

  • That is the coins greedy pays, 6, 1 and 1.
  • That is the fewest, 4 and 4.
  • That is the amount.

question 2

How many coins does greedy pay for 13 with coins of 10, 6 and 1?

the answer, and why

the answer is B

10 fits, leaving 3. 10 and 6 do not fit in 3, so three 1s: 10, 1, 1 and 1.

if you chose another

  • That is the fewest, 6, 6 and 1, which greedy misses.
  • That pays it all in 1s.
  • No two of these coins make 13.

question 3

Which coins and amount make greedy compare the most times? Each is written as the coins, then the amount.

the answer, and why

the answer is B

Neither 14 nor 13 fits in 12, so twelve 1s and 3 kinds, 15 in all.

if you chose another

  • Six 2s and 2 kinds, 8.
  • One 12 and 2 kinds, 3, the fewest any amount costs.
  • 10, 1 and 1, and 3 kinds, 6.

question 4

With coins of 1 and 4 only, why does greedy always pay the fewest coins?

the answer, and why

the answer is A

A way is fixed by its 4s, and each 4 more is 3 coins fewer. Greedy takes the most 4s that fit.

this tests where the formula comes from, on the learn page

if you chose another

  • Greedy never looks back or tries another way.
  • 5 is no number of 4s, yet greedy pays it as 4 and 1, the fewest.
  • With 1 and one other coin greedy never loses, as question 3 in section 4 of the learn page shows.

pen and paper

question 5about 8 minutes

Coins of 8, 5 and 1. Find the smallest amount where greedy pays more coins than it needs.

  1. Try amounts from 1 up, greedy against the fewest. Where does greedy first lose?
  2. At that amount, which coins does greedy pay, with how many comparisons?
  3. Fill in every way at that amount, by how many 8s and 5s it takes.
a hint

A way is fixed by its 8s and 5s; 1s pay the rest.

a worked answer

Below 8 only 5 and 1 fit, and with 1 and one other greedy never loses (learn page, section 4, question 3); 8 and 9 take one 8. At 10, 8 fits, leaving 2, and 8 and 5 do not fit in 2. Two 1s fit and a third does not: 8, 1 and 1, 3 coins, with 6 comparisons, but 5 and 5 is 2. The ways at 10 are drawn below: no 8s and no 5s take 10 coins, one 5 takes 6, two 5s take 2, and one 8 takes 3. One 8 with any 5 pays too much.

ways0 of 51 of 52 of 5
0 of 81062
1 of 83--
Every way to pay 10, counted in coins. A dash pays too much.

this tests how it works, on the learn page

what a full answer includes
  • 10: greedy pays 3 coins, and 5 and 5 is 2.
  • 8, 1 and 1, with 6 comparisons.
  • 10, 6, 2 and 3 coins.

question 6about 6 minutes

Greedy pays 1,000.

  1. At most how many comparisons can it make?
  2. At least how many?
  3. Which coins reach each?
a hint

Section 4's questions 1 and 2 count the most and the fewest.

a worked answer

At most 1,000 + 3, which is 1,003, when every coin is a 1. At least 3, on 1 and a coin of 1,000. Coins of 1, 1,001 and 1,002 reach the most, as neither big coin fits; coins of 1 and 1,000 reach the fewest.

Question 1: at most how many comparisons, CC, can greedy make paying nn, from 1 up?
  1. the move: one for each coin taken

    the line:
    one comparison foreach coin taken\begin{gathered} \text{one comparison for} \\ \text{each coin taken} \end{gathered}

    why it is allowed: A coin is taken only after it is held against what is left and fits. On the till, 6 coins, 6 that fit.

  2. the move: one for each kind

    the line:
    one more for eachkind of coin\begin{gathered} \text{one more for each} \\ \text{kind of coin} \end{gathered}

    why it is allowed: Greedy holds a coin again and again until it does not fit, once, then goes on to the next. On the till, 3 kinds, 3 that do not fit.

  3. the move: coins taken

    the line:
    coins taken:at most n\begin{gathered} \text{coins taken:} \\ \text{at most } n \end{gathered}

    why it is allowed: The coins taken add up to nn, and each is worth 1 or more. So there are at most nn of them.

  4. the move: kinds

    the line:
    kinds of coin:at most 3\begin{gathered} \text{kinds of coin:} \\ \text{at most 3} \end{gathered}

    why it is allowed: The coins come in 2 or 3 kinds, one of them 1.

  5. the move: the dearest coins reach both

    the line:
    on 1, n+1 and n+2:every coin a 1\begin{gathered} \text{on 1, } n + 1 \text{ and } n + 2\text{:} \\ \text{every coin a 1} \end{gathered}
    holdagainst what is leftfits
    64no
    54no
    14, 3, 2, 1yes, 4 times
    10no
    Paying 4 with 6, 5 and 1. 4 coins taken and 3 kinds: 7 comparisons.

    why it is allowed: Neither big coin fits in nn, so greedy pays it all in 1s, nn coins, with 3 kinds. Lines 3 and 4 are both reached.

  6. the move: add them

    the line:
    C=n+3C = n + 3

    why it is allowed: By lines 1 to 5, the most coins taken and the most kinds, which the dearest coins make. For 4, 4 + 3, which is 7.

  7. the move: a ceiling

    the line:
    C≤4nC \le 4n

    why it is allowed: From 1 up, 3 is at most 3n3n, so n+3n + 3 is at most 4n4n. For 4, 7 is at most 16.

  8. the move: read the bound

    the line:
    C∈O(n)C \in O(n)

    why it is allowed: By line 7, with the fixed multiple 4. ∈\in reads is in, and O(n)O(n) means at most a fixed multiple of nn, from some nn on.

  9. the move: a floor

    the line:
    C≥nC \ge n

    why it is allowed: n+3n + 3 is more than nn. For 4, 7 is at least 4.

  10. the move: read the bound

    the line:
    C∈Ω(n)C \in \Omega(n)

    why it is allowed: By line 9, with the fixed multiple 1. Ω(n)\Omega(n) means at least a fixed multiple of nn, above 0, from some nn on.

  11. the move: both together

    the line:
    C∈Θ(n)C \in \Theta(n)

    why it is allowed: Lines 8 and 10 bound CC by the same growth. Θ(n)\Theta(n) means both.

try it with a small nn: at n=4n = 4, line 6 gives C=7C = 7, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 1,003.
  • 3.
  • 1, 1,001 and 1,002; 1 and 1,000.