Coin row
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.
| rule | row | takes | total | the most |
|---|---|---|---|---|
| biggest first | 4, 7, 9, 2, 6, 8 | 9, 8, 4 | 21 | 21 |
| biggest first | 3, 5, 4, 1 | 5, 1 | 6 | 7 |
| every other, from coin 1 | 4, 7, 9, 2, 6, 8 | 4, 9, 6 | 19 | 21 |
| every other, from coin 2 | 4, 7, 9, 2, 6, 8 | 7, 2, 8 | 17 | 21 |
| coin | worth | take it | leave it | best |
|---|---|---|---|---|
| - | - | - | - | 0 |
| 1 | 4 | - | - | 4 |
| 2 | 7 | 7 | 4 | 7 |
| 3 | 9 | 13 | 7 | 13 |
| 4 | 2 | 9 | 13 | 13 |
| 5 | 6 | 19 | 13 | 19 |
| 6 | 8 | 21 | 19 | 21 |
| at coin | best | one back | so |
|---|---|---|---|
| 6 | 21 | 19 | taken, jump to 4 |
| 4 | 13 | 13 | left, step to 3 |
| 3 | 13 | 7 | taken, jump to 1 |
| 1 | 4 | 0 | taken, done |
step 1 of 17
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.
step 3 of 17
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
step 7 of 17
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
step 8 of 17
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
step 12 of 17
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
step 13 of 17
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.
step 14 of 17
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.
step 17 of 17
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.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- the best from no coins is 0Python line 9
- the best from the first coin is that coinPython line 10
- 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
- write the larger as the best up to this coinPython line 14
- 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
- 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 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 , which section 4 shows. The two lines stay close, as filling costs the same on both.
comparisons, ten times more at each mark up
, 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
| comparisons | time | |
|---|---|---|
| 1,000 | 1,666 | about 1.7 microseconds |
| 100,000 | 166,666 | about 167 microseconds |
| 10,000,000 | 16,666,666 | about 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 : 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.
| bestevery other coin worth 2, the last one included, the rest 1: for 5 coins, 2, 1, 2, 1, 2 | worst1, 2, 1 repeated from the last coin back: for 7 coins, 1, 1, 2, 1, 1, 2, 1 | |
|---|---|---|
| comparisons | , from 1 up | , from 1 up |
| how the comparisons grow in each case | ||
| tight, | ||
| ceiling, | ||
| floor, | ||
extra memory: a row of bests
Extra memory is the room it needs on top of the input itself, while it runs.
| row of 6 | walk back | visits | comparisons |
|---|---|---|---|
| 1, 2, 1, 2, 1, 2 | takes coins 6, 4, 2 | 3 | 8 |
| 1, 2, 1, 1, 2, 1 | leaves coin 6, takes 5, leaves 3, takes 2 | 4 | 9 |
| n | cheapest | dearest |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 4 | 5 | 6 |
| 9 | 13 | 14 |
| 100 | 149 | 166 |
| 1,000 | 1,499 | 1,666 |
where the formula comes from
Three questions, each worked line by line. Questions 1 and 2 are the counts the plot draws.
the move: filling
the line:why it is allowed: Coins 2 to each compare taking with leaving. Coin 1 is written as it is. For 6 coins, 5.
the move: never two left in a row
the line:why it is allowed: Take any coin, and call it coin . 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 equals the best two coins before it. But taking coin gives that coin plus that same best, more than it, as every coin is worth 1 or more. So the walk never does this.
guess it, thenthe move: nor coin 1
the line: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.
guess it, thenthe move: no more left than taken
the line: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.
guess it, thenthe move: how far back
the line:visit at coin best one back move 1 6 4 4 left, back 1 2 5 4 3 taken, back 2 3 3 2 2 left, back 1 4 2 2 1 taken, 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 and stops at 0, the row of no coins, or one below it after taking coin 1. For 6 coins, at most 7.
guess it, thenthe move: count each visit three times
the line: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.
guess it, thenthe move: at most
the line:why it is allowed: By line 5, twice the moves back is at most . By line 4, the coins left less the taken is at most 0. For 6 coins, at most 14.
guess it, thenthe move: divide by 3
the line:why it is allowed: Divide line 7 by 3. For 6 coins, at most 14 thirds, which is 4 and two thirds.
guess it, thenthe move: a whole number
the line:n 2n + 2 over 3 rounded down 2n over 3 rounded up 3 8 over 3 2 6 over 3 2 4 10 over 3 3 8 over 3 3 5 12 over 3 4 10 over 3 4 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 adds or takes 2 from both, so these rows cover every from 1. why it is allowed: The visits are whole, so they are at most rounded down. That is two thirds of rounded up, as the table shows. For 6 coins, 4.
guess it, thenthe move: a best grows by at most its coin
the line: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.
guess it, thenthe move: what the dearest row does
the line:coin worth best walk 7 1 5 left 6 2 5 taken 5 1 4 skipped 4 1 3 left 3 2 3 taken 2 1 1 skipped 1 1 1 taken 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.
guess it, thenthe move: two of every three
the line:n groups spare visits 2n over 3 rounded up 6 2 0 4 4 7 2 1 5 5 8 2 2 6 6 Each group of three costs 2 visits, and each spare coin 1. A spare coin adds a third of 2 to two thirds of , 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 rounded up, as the table shows. For 7 coins, 2 groups and 1 spare: 5 visits.
guess it, thenthe move: add them
the line: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.
guess it, thenthe move: a ceiling
the line:why it is allowed: Two thirds of rounded up is at most , as is whole and more than two thirds of itself. So is less than . For 6 coins, 9 is at most 12.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 14, with the fixed multiple 2. reads is in, and means at most a fixed multiple of , from some on.
guess it, thenthe move: a floor
the line:why it is allowed: From 1 up, two thirds of rounded up is at least 1. So is plus at least 1, at least . For 6 coins, 9 is at least 6.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 16, with the fixed multiple 1. means at least a fixed multiple of , above 0, from some on.
guess it, thenthe move: both together
the line:why it is allowed: Lines 15 and 17 bound by the same growth. means both.
guess it, then
try it with a small : at , line 13 gives , which you can work out by hand
the move: filling
the line:why it is allowed: As in question 1, line 1, on every row. For 5 coins, 4.
the move: at most 2 a visit
the line:why it is allowed: A take jumps back two, and a coin left steps back one.
guess it, thenthe move: how far back
the line:why it is allowed: The walk starts at coin and stops only below coin 1, at 0 or one below it. For 5 coins, at least 5.
guess it, thenthe move: so many visits
the line: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 . For 5 coins, at least 2 and a half.
guess it, thenthe move: a whole number
the line:why it is allowed: The visits are whole, so they are at least half of rounded up. For 5 coins, 3.
guess it, thenthe move: the cheapest row reaches it
the line:coin 1 2 3 4 5 worth 2 1 2 1 2 best 2 2 4 4 6 walk taken - 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 is odd, is taken by question 1's line 3. So it visits only the 2s, half of rounded up. On 2, 1, 2, 1, 2 it takes coins 5, 3 and 1: 3 visits.
guess it, thenthe move: add them
the line: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.
guess it, thenthe move: a ceiling
the line:why it is allowed: Half of rounded up is at most . So is less than . For 5 coins, 7 is at most 10.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 8, with the fixed multiple 2.
guess it, thenthe move: a floor
the line:why it is allowed: From 1 up, half of rounded up is at least 1. So is at least . For 5 coins, 7 is at least 5.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 10, with the fixed multiple 1.
guess it, thenthe move: both together
the line:why it is allowed: Lines 9 and 11 bound by the same growth.
guess it, then
try it with a small : at , line 7 gives , which you can work out by hand
the move: the promise
the line: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.
the move: it starts right
the line: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.
guess it, thenthe move: suppose so far
the line:why it is allowed: The best at coin reads only the two before it, both written earlier. Line 2 starts it, and lines 4 to 7 show the best at coin is then right too.
guess it, thenthe move: two kinds of choice
the line:why it is allowed: A choice of the first coins, none side by side, either leaves coin out, or takes it.
guess it, thenthe move: leaving it
the line:why it is allowed: Without coin , the choice uses only the coins before it. By line 3, the best one back is the most such a choice is worth.
guess it, thenthe move: taking it
the line:why it is allowed: With coin , 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.
guess it, thenthe move: so the best is right
the line:coin 3 uses worth leave it the best one back, coins 1 and 2 7 take it 9 + the best two back, coin 1 13 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 . For coin 3 of the first example: leaving it is worth 7, and taking it 9 + 4, which is 13, so 13.
guess it, thenthe move: every best
the line: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.
guess it, thenthe move: the walk names the coins
the line: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.
guess it, thenthe move: count them
the line:why it is allowed: One best for no coins, then one for each coin. For the first example's 6 coins, 7.
guess it, then
try it with a small : at , line 10 gives , 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.
| step | the row | what happens | comparisons |
|---|---|---|---|
| 1 | 0 | Take the most money from a row of 4 coins, never two side by side. With no coins you have 0, so write 0 first.0 | 0 |
| 2 | 0, 3 | With only the first coin, take it: 3. Write 3.0, 3 | 0 |
| 3 | 0, 3 | Coin 2 is worth 5. Take it, plus the best two back, 0: 5. Or leave it and keep the best one back: 3.0, 3 | 1 |
| 4 | 0, 3, 5 | Taking coin 2 gives more, so the best from the first 2 coins is 5. Write 5.0, 3, 5 | 1 |
| 5 | 0, 3, 5 | Coin 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, 5 | 2 |
| 6 | 0, 3, 5, 7 | Taking coin 3 gives more, so the best from the first 3 coins is 7. Write 7.0, 3, 5, 7 | 2 |
| 7 | 0, 3, 5, 7 | Coin 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, 7 | 3 |
| 8 | 0, 3, 5, 7, 7 | Taking coin 4 gives no more, so leave it. The best from the first 4 coins stays 7. Write 7.0, 3, 5, 7, 7 | 3 |
| 9 | 0, 3, 5, 7, 7 | Walking 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, 7 | 4 |
| 10 | 0, 3, 5, 7, 7 | Walking 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, 7 | 5 |
| 11 | 0, 3, 5, 7, 7 | Walking back: the best at coin 1 is 3, more than 0, so coin 1 was taken.0, 3, 5, 7, 7 | 6 |
| 12 | 0, 3, 5, 7, 7 | Done: the most money is 7. Take 3 and 4.0, 3, 5, 7, 7 | 6 |
In all, 6 comparisons. The formula from section 4, , gives at : 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 . 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 comparisons.
| coins | the choices | how many |
|---|---|---|
| 1 | none; coin 1 | 2 |
| 2 | none; coin 1; coin 2 | 3 |
| 3, leaves coin 3 | none; coin 1; coin 2 | 3 |
| 3, takes coin 3 | coin 3; coins 1 and 3 | 2 |
| coins | trying every choice | the row of bests, at most |
|---|---|---|
| 9 | 89 choices | 14 comparisons |
| 20 | 17,711 choices | 33 comparisons |
| 30 | 2,178,309 choices | 49 comparisons |