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.

how it works

The idea on the watch page's first example, then the whole method on it, step by step.

A row of coins: 4, 7, 9, 2, 6, 8. Take any you like, but never two side by side. What is the most money? Two quick rules fail, as the first table shows: biggest first, and every other coin. So answer smaller rows first. The best from no coins is 0, and from the first coin it is that coin. For each later coin there are two choices. Take it: the coin before it is out, so add it to the best two back. Leave it: keep the best one back. Write the larger. For coin 3, worth 9: taking it is 9 plus 4, 13; leaving it keeps 7. The best is 13. The last best is the answer, 21. To see which coins, walk back from the last. A best bigger than the one before means that coin was taken, so jump back two. The same means it was left, so step back one. This page counts comparisons. Filling makes one for each coin from the 2nd, and the walk one for each coin it visits. Here filling makes 5, for coins 2 to 6, and the walk 4, at coins 6, 4, 3 and 1: 9 in all, as the watch page counts. The code below fills the bests, walks back from the last coin and returns the coins taken.

rulerowtakestotalthe most
biggest first4, 7, 9, 2, 6, 89, 8, 42121
biggest first3, 5, 4, 15, 167
every other, from coin 14, 7, 9, 2, 6, 84, 9, 61921
every other, from coin 24, 7, 9, 2, 6, 87, 2, 81721
Two quick rules. Biggest first works on the first example, but on 3, 5, 4, 1 it takes 5, then only 1 is free: 6, where 3 and 4 make 7. Every other coin misses 21 either way.
coinworthtake itleave itbest
----0
14--4
27747
3913713
4291313
56191319
68211921
The first example, filled from the top. Take it is the coin plus the best two rows up; leave it is the best one row up. The dash row is no coins. The last best, 21, is the answer.
at coinbestone backso
62119taken, jump to 4
41313left, step to 3
3137taken, jump to 1
140taken, done
Walking back. Coins 1, 3 and 6 were taken, worth 4, 9 and 8, which make 21.
Two rich coins side by side: 8 moments of its 17 steps, the steps between them left for the watch page
  1. step 1 of 17

    479268F0

    Take the most money from a row of 6 coins, never two side by side. With no coins you have 0, so write 0 first.

    what it remembers to know where it is
    at coin
    not yet
    if taken
    not yet
    if left
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  2. step 3 of 17

    479268F04

    Coin 2 is worth 7. Take it, plus the best two back, 0: 7. Or leave it and keep the best one back: 4.

    what it remembers to know where it is
    at coin
    2
    if taken
    7
    if left
    4

    watch from here

  3. step 7 of 17

    479268F04713

    Coin 4 is worth 2. Take it, plus the best two back, 7: 9. Or leave it and keep the best one back: 13.

    what it remembers to know where it is
    at coin
    4
    if taken
    9
    if left
    13

    watch from here

  4. step 8 of 17

    479268F0471313

    Taking coin 4 gives no more, so leave it. The best from the first 4 coins stays 13. Write 13.

    what it remembers to know where it is
    at coin
    4
    if taken
    9
    if left
    13

    watch from here

  5. step 12 of 17

    479268F04713131921

    Taking coin 6 gives more, so the best from the first 6 coins is 21. Write 21.

    what it remembers to know where it is
    at coin
    6
    if taken
    21
    if left
    19

    watch from here

  6. step 13 of 17

    479268F04713131921

    Walking back: the best at coin 6 is 21, more than 19 one back, so coin 6, worth 8, was taken. The coin before it cannot be, so jump back two.

    what it remembers to know where it is
    at coin
    6
    if taken
    not yet
    if left
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  7. step 14 of 17

    479268F04713131921

    Walking back: the best at coin 4 is 13, the same as one back, so coin 4 was left. Step back one.

    what it remembers to know where it is
    at coin
    4
    if taken
    not yet
    if left
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  8. step 17 of 17

    479268F04713131921

    Done: the most money is 21. Take 4, 9 and 8.

    what it remembers to know where it is
    at coin
    not yet
    if taken
    not yet
    if left
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

the steps, in plain words
  1. the best from no coins is 0Python line 9
  2. the best from the first coin is that coinPython line 10
  3. for each later coin, compare taking it, the coin plus the best two back, with leaving it, the best one backPython lines 11, 12 and 13
  4. write the larger as the best up to this coinPython line 14
  5. walk back from the last coin; where the best is more than one back, that coin was taken, so jump back two, else step back onePython lines 15, 16, 17, 18, 19, 20, 21 and 22
  6. the last best is the most money, and the coins the walk took make itPython line 23
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

def coin_row(coins):
    """The coins that make the most money from a row, never two side by side.

    coins is a list of at least one whole number from 1 up. Fills best,
    where best[i] is the most money from the first i coins, then walks
    back from the last coin and returns the coins taken, left to right.
    """
    n = len(coins)
    best = [0] * (n + 1)  # no coins: 0
    best[1] = coins[0]  # the first coin alone: take it
    for i in range(2, n + 1):  # each later coin, from the left
        take = coins[i - 1] + best[i - 2]  # this coin, plus the best two back
        leave = best[i - 1]  # or the best one back
        best[i] = max(take, leave)  # write the larger
    taken = []
    i = n
    while i >= 1:  # walk back from the last coin
        if best[i] > best[i - 1]:  # more than one back: this coin was taken
            taken.insert(0, coins[i - 1])
            i -= 2  # the coin before it cannot be
        else:  # the same as one back: this coin was left
            i -= 1
    return taken  # the coins that make best[n], the most money

how it scales

The plot counts the comparisons for a row of nn coins, measured from 10 to 100,000. Two rows: the cheapest, which costs the fewest comparisons, and the dearest, which costs the most, both drawn in section 3.

Each mark across or up is ten times the one before. Both lines climb one mark up for each mark across: 149 and 166 comparisons at 100, 1,499 and 1,666 at 1,000. Ten times the coins, ten times the comparisons: a slope of 1, as the counts grow like nn, which section 4 shows. The two lines stay close, as filling costs the same on both.

comparisons, ten times more at each mark up

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, how many coins are in the row, ten times more at each mark to the right

  • 1, 2, 1 repeated from the last coin back: for 7 coins, 1, 1, 2, 1, 1, 2, 1solid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
  • every other coin worth 2, the last one included, the rest 1: for 5 coins, 2, 1, 2, 1, 2solid where measured by running it; dashed where its formula carries it on to ten million
what a dearest row costs, at a billion comparisons a second
nncomparisonstime
1,0001,666about 1.7 microseconds
100,000166,666about 167 microseconds
10,000,00016,666,666about 17 milliseconds

The comparisons are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count the coin row's comparisons yourself, as the plot on this page does.

Run it with Python 3.12 or later. For each row of n coins from 10 to
100,000 it prints the comparisons the method makes on the cheapest row
and on the dearest: filling the table, then walking back to the coins
taken. The counts are the same on any computer; only the time differs.
"""


def coin_row(coins):
    """The coins that make the most money from a row, never two side by side.

    coins is a list of at least one whole number from 1 up. Fills best,
    where best[i] is the most money from the first i coins, then walks
    back from the last coin and returns the coins taken, left to right.
    """
    n = len(coins)
    best = [0] * (n + 1)  # no coins: 0
    best[1] = coins[0]  # the first coin alone: take it
    for i in range(2, n + 1):  # each later coin, from the left
        take = coins[i - 1] + best[i - 2]  # this coin, plus the best two back
        leave = best[i - 1]  # or the best one back
        best[i] = max(take, leave)  # write the larger
    taken = []
    i = n
    while i >= 1:  # walk back from the last coin
        if best[i] > best[i - 1]:  # more than one back: this coin was taken
            taken.insert(0, coins[i - 1])
            i -= 2  # the coin before it cannot be
        else:  # the same as one back: this coin was left
            i -= 1
    return taken  # the coins that make best[n], the most money


def count(coins):
    """How many comparisons coin_row makes on this row: one for each coin
    from the 2nd while filling the table, then one for each coin the walk
    back visits."""
    n = len(coins)
    best = [0] * (n + 1)  # no coins: 0
    best[1] = coins[0]  # the first coin alone
    comparisons = 0
    for i in range(2, n + 1):  # each later coin
        comparisons += 1  # take it, or leave it
        best[i] = max(coins[i - 1] + best[i - 2], best[i - 1])
    i = n
    while i >= 1:  # walk back from the last coin
        comparisons += 1  # more than one back, or the same
        if best[i] > best[i - 1]:  # this coin was taken
            i -= 2  # the coin before it was not
        else:  # this coin was left
            i -= 1
    return comparisons


def cheapest(n):
    """Every other coin worth 2, the last one included, the rest 1: the
    walk back takes every coin it visits, jumping back two each time."""
    return [2 if (n - 1 - k) % 2 == 0 else 1 for k in range(n)]


def dearest(n):
    """1, 2, 1 repeated from the last coin back: the walk back leaves a
    coin, takes the next, and skips one, again and again."""
    return [2 if (n - 1 - k) % 3 == 1 else 1 for k in range(n)]


if __name__ == "__main__":
    print(f"{'n':>7} {'cheapest':>9} {'dearest':>9}")
    for n in (10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10_000, 20_000, 50_000, 100_000):
        print(f"{n:>7} {count(cheapest(n)):>9} {count(dearest(n)):>9}")

best, worst and average

Filling costs the same on every row, one comparison for each coin from the 2nd. The walk back is what differs: a coin taken moves it back two, a coin left only one. So the cheapest row has the walk take every coin it visits, and the dearest leaves as many as it can.

A case is one kind of input, the thing the method is given, such as a row back to front, whatever its length. A bound says how fast that case's cost grows as the input gets longer.

The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and the fastest-growing piece swamps the rest. Here both pieces grow like nn: at 1,000 coins the dearest row makes 999 comparisons to fill and 667 to walk back. So how a cost grows is what tells one method from another, and the bounds keep just that.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
bestevery other coin worth 2, the last one included, the rest 1: for 5 coins, 2, 1, 2, 1, 2worst1, 2, 1 repeated from the last coin back: for 7 coins, 1, 1, 2, 1, 1, 2, 1
comparisonsn−1+⌈n2⌉n - 1 + \lceil \frac{n}{2} \rceil, from 1 upn−1+⌈2n3⌉n - 1 + \lceil \frac{2n}{3} \rceil, from 1 up
how the comparisons grow in each case
tight, Θ\ThetaΘ(n)\Theta(n)Θ(n)\Theta(n)
ceiling, OOO(n)O(n)O(n)O(n)
floor, Ω\OmegaΩ(n)\Omega(n)Ω(n)\Omega(n)

extra memory: a row of n+1n + 1 bests

Extra memory is the room it needs on top of the input itself, while it runs.

row of 6walk backvisitscomparisons
1, 2, 1, 2, 1, 2takes coins 6, 4, 238
1, 2, 1, 1, 2, 1leaves coin 6, takes 5, leaves 3, takes 249
The cheapest and the dearest rows of 6 coins, counted by the run. Both fill with 5 comparisons; the walk adds the rest.
ncheapestdearest
111
223
456
91314
100149166
1,0001,4991,666
Comparisons on the cheapest and the dearest row of nn coins, counted by the run. They stay close, 167 apart at 1,000.

where the formula comes from

Three questions, each worked line by line. Questions 1 and 2 are the counts the plot draws.

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

Question 2: at least how many comparisons, BB, does 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: As in question 1, line 1, on every row. For 5 coins, 4.

  2. the move: at most 2 a visit

    the line:
    each visit moves thewalk back at most 2\begin{gathered} \text{each visit moves the} \\ \text{walk back at most 2} \end{gathered}

    why it is allowed: A take jumps back two, and a coin left steps back one.

  3. the move: how far back

    the line:
    moves back: at least n\text{moves back: at least } n

    why it is allowed: The walk starts at coin nn and stops only below coin 1, at 0 or one below it. For 5 coins, at least 5.

  4. the move: so many visits

    the line:
    visits: at least n2\text{visits: at least } \frac{n}{2}

    why it is allowed: By line 2, the visits move the walk back at most twice their number, and by line 3 that must reach nn. For 5 coins, at least 2 and a half.

  5. the move: a whole number

    the line:
    visits: at least ⌈n2⌉\text{visits: at least } \lceil \frac{n}{2} \rceil

    why it is allowed: The visits are whole, so they are at least half of nn rounded up. For 5 coins, 3.

  6. the move: the cheapest row reaches it

    the line:
    every other coin 2,the last included\begin{gathered} \text{every other coin 2,} \\ \text{the last included} \end{gathered}
    coin12345
    worth21212
    best22446
    walktaken-taken-taken
    The cheapest row of 5. Each best at a coin worth 2 is more than the one before, so each is taken and the walk jumps two.

    why it is allowed: At a 2, the coin before is a 1, so by question 1's line 10 the best one back is at most 1 more than the best two back. Taking gives 2 more: the 2 is taken, and the walk jumps to the next 2. Coin 1, a 2 when nn is odd, is taken by question 1's line 3. So it visits only the 2s, half of nn rounded up. On 2, 1, 2, 1, 2 it takes coins 5, 3 and 1: 3 visits.

  7. the move: add them

    the line:
    B=n−1+⌈n2⌉B = n - 1 + \lceil \frac{n}{2} \rceil

    why it is allowed: By lines 1, 5 and 6, filling and the fewest visits, which the cheapest row makes. For 5 coins, 4 + 3, which is 7.

  8. the move: a ceiling

    the line:
    B≤2nB \le 2n

    why it is allowed: Half of nn rounded up is at most nn. So BB is less than 2n2n. For 5 coins, 7 is at most 10.

  9. the move: read the bound

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

    why it is allowed: By line 8, with the fixed multiple 2.

  10. the move: a floor

    the line:
    B≥nB \ge n

    why it is allowed: From 1 up, half of nn rounded up is at least 1. So BB is at least nn. For 5 coins, 7 is at least 5.

  11. the move: read the bound

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

    why it is allowed: By line 10, with the fixed multiple 1.

  12. the move: both together

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

    why it is allowed: Lines 9 and 11 bound BB by the same growth.

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

Question 3: why is every best right, and how many are there? Let KK be how many bests the row holds, for nn coins.
  1. the move: the promise

    the line:
    each best is the mostfrom its first coins,none side by side\begin{gathered} \text{each best is the most} \\ \text{from its first coins,} \\ \text{none side by side} \end{gathered}

    why it is allowed: This is what to prove. Lines 2 to 8 prove it, best by best, from the left, and line 9 shows the walk names coins that make the last best.

  2. the move: it starts right

    the line:
    the bests of no coinsand of coin 1 are right\begin{gathered} \text{the bests of no coins} \\ \text{and of coin 1 are right} \end{gathered}

    why it is allowed: With no coins the most is 0. With one coin, taking it beats leaving it, as it is worth 1 or more.

  3. the move: suppose so far

    the line:
    suppose every best beforecoin i is right, from coin 2 on\begin{gathered} \text{suppose every best before} \\ \text{coin } i \text{ is right, from coin 2 on} \end{gathered}

    why it is allowed: The best at coin ii reads only the two before it, both written earlier. Line 2 starts it, and lines 4 to 7 show the best at coin ii is then right too.

  4. the move: two kinds of choice

    the line:
    a choice leavescoin i or takes it\begin{gathered} \text{a choice leaves} \\ \text{coin } i \text{ or takes it} \end{gathered}

    why it is allowed: A choice of the first ii coins, none side by side, either leaves coin ii out, or takes it.

  5. the move: leaving it

    the line:
    leaving it: worth atmost the best one back\begin{gathered} \text{leaving it: worth at} \\ \text{most the best one back} \end{gathered}

    why it is allowed: Without coin ii, the choice uses only the coins before it. By line 3, the best one back is the most such a choice is worth.

  6. the move: taking it

    the line:
    taking it: worth at mostthe coin + the best two back\begin{gathered} \text{taking it: worth at most} \\ \text{the coin + the best two back} \end{gathered}

    why it is allowed: With coin ii, the coin before it is out, so the rest uses only the coins before that one. By line 3, the best two back is the most the rest is worth.

  7. the move: so the best is right

    the line:
    the larger of the twois the most\begin{gathered} \text{the larger of the two} \\ \text{is the most} \end{gathered}
    coin 3usesworth
    leave itthe best one back, coins 1 and 27
    take it9 + the best two back, coin 113
    The two kinds of choice for coin 3 of the first example. The larger is 13, and no choice of coins 1 to 3 is worth more.

    why it is allowed: By lines 5 and 6, no choice is worth more than the larger. Both can be made: the choice behind the best one back, or the one behind the best two back plus coin ii. For coin 3 of the first example: leaving it is worth 7, and taking it 9 + 4, which is 13, so 13.

  8. the move: every best

    the line:
    every best is right\text{every best is right}

    why it is allowed: By line 2, the first two are. By lines 3 to 7, each later one is right once those before it are. So, filled from the left, every best is, the last included.

  9. the move: the walk names the coins

    the line:
    the coins the walk takesmake the last best,none side by side\begin{gathered} \text{the coins the walk takes} \\ \text{make the last best,} \\ \text{none side by side} \end{gathered}

    why it is allowed: A best more than the one before did not come from leaving, so it is its coin plus the best two back. The walk takes the coin, and the rest of the money is the best where it lands. A best the same as the one before is made without its coin: the rest is the best one back. So the coins taken, plus the best where the walk stands, always make the last best. Coin 1's best is the coin alone, so once the walk passes coin 1, nothing is left. Each take jumps over the coin before it.

  10. the move: count them

    the line:
    K=n+1K = n + 1

    why it is allowed: One best for no coins, then one for each coin. For the first example's 6 coins, 7.

try it with a small nn: at n=4n = 4, line 10 gives K=5K = 5, which you can work out by hand

count it yourself

Four coins, 3, 5, 4, 2. Its walk visits 3 of the 4 coins, the most a row of 4 allows, so it is one of the dearest rows of 4. Step by step, each comparison adds 1 to the count on its own step.

3, 5, 4, 2, every step written down
stepthe rowwhat happenscomparisons
10Take the most money from a row of 4 coins, never two side by side. With no coins you have 0, so write 0 first.00
20, 3With only the first coin, take it: 3. Write 3.0, 30
30, 3Coin 2 is worth 5. Take it, plus the best two back, 0: 5. Or leave it and keep the best one back: 3.0, 31
40, 3, 5Taking coin 2 gives more, so the best from the first 2 coins is 5. Write 5.0, 3, 51
50, 3, 5Coin 3 is worth 4. Take it, plus the best two back, 3: 7. Or leave it and keep the best one back: 5.0, 3, 52
60, 3, 5, 7Taking coin 3 gives more, so the best from the first 3 coins is 7. Write 7.0, 3, 5, 72
70, 3, 5, 7Coin 4 is worth 2. Take it, plus the best two back, 5: 7. Or leave it and keep the best one back: 7.0, 3, 5, 73
80, 3, 5, 7, 7Taking coin 4 gives no more, so leave it. The best from the first 4 coins stays 7. Write 7.0, 3, 5, 7, 73
90, 3, 5, 7, 7Walking back: the best at coin 4 is 7, the same as one back, so coin 4 was left. Step back one.0, 3, 5, 7, 74
100, 3, 5, 7, 7Walking back: the best at coin 3 is 7, more than 5 one back, so coin 3, worth 4, was taken. The coin before it cannot be, so jump back two.0, 3, 5, 7, 75
110, 3, 5, 7, 7Walking back: the best at coin 1 is 3, more than 0, so coin 1 was taken.0, 3, 5, 7, 76
120, 3, 5, 7, 7Done: the most money is 7. Take 3 and 4.0, 3, 5, 7, 76

In all, 6 comparisons. The formula from section 4, n−1+⌈2n3⌉n - 1 + \lceil \frac{2n}{3} \rceil, gives C=6C = 6 at n=4n = 4: the same.

where you meet it

The coin row is a small puzzle, set in interviews and textbooks.

  • An interview puzzle

    LeetCode, a site of programming puzzles, sets it as House Robber. Houses stand in a row, each holding some money, and an alarm rings if two side by side are robbed. This page's row of bests answers it.

    source: LeetCode, problem 198, House Robber

  • Its name in a textbook

    Anany Levitin's textbook on designing algorithms calls it the coin-row problem, and solves it with a row of bests as a first example of planning ahead.

    source: A. Levitin, Introduction to the Design and Analysis of Algorithms, 3rd edition, Pearson, 2012, section 8.1

  • Jobs that clash with their neighbours

    Weighted interval scheduling picks paid jobs whose times do not overlap, for the most pay. Kleinberg and Tardos's textbook sorts the jobs by end time and fills a table with the same two choices. Take a job, plus the best of the jobs that end before it starts, or leave it. When each job clashes only with its neighbours, that is this page's rule.

    source: J. Kleinberg and É. Tardos, Algorithm Design, Pearson, 2005, section 6.1

what is used instead

Jobs of different lengths can clash with more than their neighbours. Sorted by end time, they still give a row of bests, as Kleinberg and Tardos show (section 6.1). Each job then reads the best of the jobs that end before it starts, not always the one two back.

how it compares

The plain method tries every choice of coins with none side by side. The table shows the choices for 1, 2 and 3 coins. A choice of 3 coins either leaves coin 3, one of the 3 choices of 2 coins, or takes it, one of the 2 choices of 1 coin. So each count adds the two before it: 2, 3, 5, 8 and on. Each count is then at least twice the one two before, so two more coins at least double it, which passes any fixed multiple of nn. At 10 coins, 144 choices pass 10 times 10, which is 100. The row of bests reads each smaller answer once, as the page on solving each smaller problem once shows, and makes at most 2n2n comparisons.

coinsthe choiceshow many
1none; coin 12
2none; coin 1; coin 23
3, leaves coin 3none; coin 1; coin 23
3, takes coin 3coin 3; coins 1 and 32
Every choice with none side by side, for the first 1, 2 and 3 coins. The choices that leave coin 3 are those of 2 coins; those that take it are those of 1 coin, with coin 3 added. 3 and 2, 5 in all.
coinstrying every choicethe row of bests, at most
989 choices14 comparisons
2017,711 choices33 comparisons
302,178,309 choices49 comparisons
Each count of choices adds the two before it. A choice and a comparison are different steps, so compare how each grows, not one number with the other.