Skip to content

Coin row

planning ahead

Level 2 of 3

Also called Coin-row problem and Most money with no two neighbours, by dynamic programming.

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 the method make on the row 6, 2, 9, 4, 3?

the answer, and why

the answer is B

4 to fill, one for each coin from the 2nd. Then the walk takes coins 5, 3 and 1, jumping back two each time, 3 more.

if you chose another

  • That is filling alone; the walk back compares too.
  • That is one for each coin, but filling skips coin 1 and the walk skips coins 4 and 2.
  • That counts the walk visiting every coin, but each take skips the coin before it.

question 2

What is the most money from the row 6, 9, 5, 1, 8, 4, never two coins side by side?

the answer, and why

the answer is D

The bests run 0, 6, 9, 11, 11, 19, 19. Coins 6, 5 and 8 make 19.

this tests how it works, on the learn page

if you chose another

  • That is biggest first, 9 and then 8, which shuts out 6 and 5.
  • That is the 2nd, 4th and 6th coins.
  • That takes 6 and 9, which sit side by side.

question 3

Which row makes the method compare the most times?

the answer, and why

the answer is A

7 coins: 6 to fill, and the walk takes coins 7, 5, 3 and 1, 4 more, 10 in all. The cheapest row of 7 still costs more than the dearest of 6.

if you chose another

  • The dearest row of 6, 5 + 4, which is 9. One coin fewer outweighs a dearer walk.
  • 4 to fill, and the walk takes coins 5, 3 and 1, 7 in all.
  • 1 to fill, and the walk leaves coin 2 and takes coin 1, 3 in all.

question 4

Walking back on the watch page's first example, 4, 7, 9, 2, 6, 8, the walk takes coin 6. Which coin does it look at next?

the answer, and why

the answer is A

Coin 6 was taken, so coin 5 cannot be, and the walk jumps back two, to coin 4.

this tests how it works, on the learn page

if you chose another

  • Coin 5 sits beside coin 6, so it is skipped, not looked at.
  • Coin 3 comes after coin 4 is found to be left.
  • Coin 1 is the last the walk looks at.

pen and paper

question 5about 8 minutes

Run the method by hand on the row 4, 9, 7, 2.

  1. Fill the row of bests.
  2. What is the most money, and how many comparisons did you make?
  3. Walk back from the last coin, leaving a coin where taking ties with leaving: which coins does it take?
a hint

The best of no coins is 0 and of coin 1 is that coin. Then take the larger of the coin plus the best two back, and the best one back.

a worked answer

The bests are drawn below. The last is 11, the most money. Filling makes 3 comparisons. Walking back: at coin 4 the best is 11, the same as one back, so coin 4 is left. At coin 3 it is 11, more than 9, so coin 3 is taken: jump to coin 1. At coin 1 it is 4, more than the best of no coins, so coin 1 is taken. 3 more comparisons, 6 in all. Take 4 and 7.

coinworthtake itleave itbest
----0
14--4
29949
3711911
42111111
The row 4, 9, 7, 2. At coin 4, taking ties with leaving, so the best stays 11 and the walk leaves it.

this tests how it works, on the learn page

what a full answer includes
  • The bests 0, 4, 9, 11, 11.
  • 11, and 6 comparisons.
  • Coins 1 and 3, worth 4 and 7.

question 6about 6 minutes

A row has 300 coins.

  1. At most how many comparisons can the method make?
  2. At least how many?
  3. Which row of 300 reaches each?
a hint

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

a worked answer

At most 299 + 200, which is 499, as two thirds of 300 is 200. At least 299 + 150, which is 449, as half of 300 is 150. The row 1, 2, 1 repeated from the last coin back reaches the most, and every other coin worth 2, ending with the last, the fewest.

Question 1: at most how many comparisons, CC, can a row of nn coins cost, from 1 up?
  1. the move: filling

    the line:
    filling: one a coinfrom the 2nd, n−1\begin{gathered} \text{filling: one a coin} \\ \text{from the 2nd, } n - 1 \end{gathered}

    why it is allowed: Coins 2 to nn each compare taking with leaving. Coin 1 is written as it is. For 6 coins, 5.

  2. the move: never two left in a row

    the line:
    the walk never leavestwo coins in a row\begin{gathered} \text{the walk never leaves} \\ \text{two coins in a row} \end{gathered}

    why it is allowed: Take any coin, and call it coin ii. Say the walk left it, then the coin before it. Leaving a coin means its best equals the one before, so the best at coin ii equals the best two coins before it. But taking coin ii gives that coin plus that same best, more than it, as every coin is worth 1 or more. So the walk never does this.

  3. the move: nor coin 1

    the line:
    nor coin 1, so itslast visit takes\begin{gathered} \text{nor coin 1, so its} \\ \text{last visit takes} \end{gathered}

    why it is allowed: The best up to coin 1 is the coin, more than 0, so the walk takes coin 1. A walk that leaves coin 2 then visits coin 1 and takes it.

  4. the move: no more left than taken

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

    why it is allowed: By lines 2 and 3, the coin visited just after a coin left is taken. So each coin left has a take of its own after it.

  5. the move: how far back

    the line:
    moves back: atmost n+1\begin{gathered} \text{moves back: at} \\ \text{most } n + 1 \end{gathered}
    visitat coinbestone backmove
    1644left, back 1
    2543taken, back 2
    3322left, back 1
    4221taken, back 2
    The walk on 1, 2, 1, 1, 2, 1. Each coin left is followed by a take. 2 left, 2 taken, moving back 6 in all.

    why it is allowed: A coin left moves the walk back 1, and a take 2. It starts at coin nn and stops at 0, the row of no coins, or one below it after taking coin 1. For 6 coins, at most 7.

  6. the move: count each visit three times

    the line:
    3 times the visits is twicethe moves back, plus thecoins left, less the taken\begin{gathered} \text{3 times the visits is twice} \\ \text{the moves back, plus the} \\ \text{coins left, less the taken} \end{gathered}

    why it is allowed: A coin left is 1 visit and moves back 1: 3 is twice 1, plus 1. A take is 1 visit and moves back 2: 3 is twice 2, less 1. Add these over every visit. For the walk in line 5's picture: 3 times 4 is twice 6, plus 2, less 2.

  7. the move: at most

    the line:
    3 times the visits:at most 2(n+1)\begin{gathered} \text{3 times the visits:} \\ \text{at most } 2(n + 1) \end{gathered}

    why it is allowed: By line 5, twice the moves back is at most 2(n+1)2(n + 1). By line 4, the coins left less the taken is at most 0. For 6 coins, at most 14.

  8. the move: divide by 3

    the line:
    visits: at most2n+23\begin{gathered} \text{visits: at most} \\ \frac{2n + 2}{3} \end{gathered}

    why it is allowed: Divide line 7 by 3. For 6 coins, at most 14 thirds, which is 4 and two thirds.

  9. the move: a whole number

    the line:
    visits: at most⌈2n3⌉\begin{gathered} \text{visits: at most} \\ \lceil \frac{2n}{3} \rceil \end{gathered}
    n2n + 2 over 3rounded down2n over 3rounded up
    38 over 326 over 32
    410 over 338 over 33
    512 over 3410 over 34
    2n2n leaves 0, 2 or 1 over when divided by 3, as for 3, 4 and 5. In each, the two last columns agree. Adding or taking 3 from nn adds or takes 2 from both, so these rows cover every nn from 1.

    why it is allowed: The visits are whole, so they are at most 2n+23\frac{2n + 2}{3} rounded down. That is two thirds of nn rounded up, as the table shows. For 6 coins, 4.

  10. the move: a best grows by at most its coin

    the line:
    a best is at most its coinplus the best one back\begin{gathered} \text{a best is at most its coin} \\ \text{plus the best one back} \end{gathered}

    why it is allowed: Taking a coin gives it plus the best two back, and the best two back is at most the best one back, as leaving the coin keeps the old best. Leaving gives the best one back. Coin 1's best is the coin itself, its coin more than 0. For coin 3 of the first example, 13 is at most 9 plus 7.

  11. the move: what the dearest row does

    the line:
    on 1, 2, 1 repeated fromthe last coin: each 2 taken,each 1 just right of a 2 left\begin{gathered} \text{on 1, 2, 1 repeated from} \\ \text{the last coin: each 2 taken,} \\ \text{each 1 just right of a 2 left} \end{gathered}
    coinworthbestwalk
    715left
    625taken
    514skipped
    413left
    323taken
    211skipped
    111taken
    The dearest row of 7, read from the last coin. Each 2 is taken and the 1 just right of it left; the 1 before it is skipped (jumped over, not visited). Coin 7: taking gives 1 + 4, which is 5, and leaving keeps 5. The same, so it is left. 5 visits.

    why it is allowed: At a 2, the coin before is a 1, so by line 10 the best one back is at most 1 more than the best two back. Taking gives 2 more: the 2 is taken. At a 1 just right of a 2, taking gives 1 plus the best two back. The coin two back is a 1, so by line 10 that is at most 2 plus the best three back. Taking that 2 already gave as much: the 1 is left. Where coin 1 is the 2, line 3 takes it, and coin 2 gives 1, less than 2, so it is left.

  12. the move: two of every three

    the line:
    the dearest row’s walkvisits ⌈2n3⌉ coins\begin{gathered} \text{the dearest row's walk} \\ \text{visits } \lceil \frac{2n}{3} \rceil \text{ coins} \end{gathered}
    ngroupssparevisits2n over 3 rounded up
    62044
    72155
    82266
    Each group of three costs 2 visits, and each spare coin 1. A spare coin adds a third of 2 to two thirds of nn, which rounds up to the visit it costs.

    why it is allowed: By line 11, from the last coin each 1, 2, 1 costs 2 visits: the 1 left, the 2 taken, the next 1 skipped. One spare coin at the start, coin 1, is visited and taken (line 3). Two spare are a 2 then a 1. The best at coin 2 is 2, the same as at coin 1, so coin 2 is left and coin 1 taken (line 3), both visited. That is two thirds of nn rounded up, as the table shows. For 7 coins, 2 groups and 1 spare: 5 visits.

  13. the move: add them

    the line:
    C=n−1+⌈2n3⌉C = n - 1 + \lceil \frac{2n}{3} \rceil

    why it is allowed: By lines 1, 9 and 12, filling and the most visits, which the dearest row makes. For 6 coins, 5 + 4, which is 9.

  14. the move: a ceiling

    the line:
    C≤2nC \le 2n

    why it is allowed: Two thirds of nn rounded up is at most nn, as nn is whole and more than two thirds of itself. So CC is less than 2n2n. For 6 coins, 9 is at most 12.

  15. the move: read the bound

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

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

  16. the move: a floor

    the line:
    C≥nC \ge n

    why it is allowed: From 1 up, two thirds of nn rounded up is at least 1. So CC is n−1n - 1 plus at least 1, at least nn. For 6 coins, 9 is at least 6.

  17. the move: read the bound

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

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

  18. the move: both together

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

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

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

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

what a full answer includes
  • 499.
  • 449.
  • 1, 2, 1 repeated from the last coin back; every other coin 2, the last included.