Coins in a Line
This game is a tricky little coding problem that works has the following rules:
- There are an even number of coins in a line, with values , i.e. is the value of the i-th coin.
- Two players, often called Alice and Bob, take turns to take a coin either from the left or the right end of the line until there are no more coins left.
- The player whose coins have the higher total value wins.
The goal is to find an algorithm that maximizes the value of the coins that the first player (Alice) gets.
There are 4 coins with values [1, 2, 3, 4], Alice will get the maximum value of 6 by taking the last coin twice (4 + 2), assuming Bob also plays optimally.
Greedy Algorithm
This game isn't as simple as it seems, and it's not immediately obvious how to solve it. Most commonly people will start with a greedy algorithm, which is to take the coin with the highest value at each turn. This is a good start, and will win in the example above, but it's not optimal. Consider the following example:
There are again 4 coins but with the values [5, 10, 25, 10].
- Alice takes the right coin with value 10.
- Bob takes the right coin with value 25.
- Alice takes the right coin with value 10.
- Bob takes the last coin with value 5.
Alice will have a total value of 20, and Bob will have a total value of 30. Bob wins!
By tweaking the greedy algorithm, we can get an algorithm that will always win, but not necessarily get the maximum value. Instead of taking the coin with the highest value, Alice first calculates the total value of coins in the odd positions, and then calculates the total value of coins in the even positions (starting at 0). She then takes the coin in the positions with the highest total sum.
There are again 6 coins but with the values [1,3,6,3,1,3]. First Alice calculates the total value of coins in the even positions, which is 1 + 6 + 1 = 8. Then she calculates the total value of coins in the odd positions, which is 3 + 3 + 3 = 9. So she takes the coins in the odd positions. If Bob uses the greedy approach we get the following:
- Alice takes the right coin with value 3 (original position=5).
- Bob takes the left coin with value 1.
- Alice takes the left coin with value 3 (original position=1).
- Bob takes the left coin with value 6.
- Alice takes the left coin with value 3 (original position=3).
- Bob takes the last coin with value 1.
Alice will have a total value of 9, and Bob will have a total value of 8. Alice wins, but there is a way to get 10!
If Bob uses the same tweaked greedy approach as Alice, we get the following:
- Alice takes the right coin with value 3 (original position=5).
- Bob can't take an odd position coin, so he can take either coin as they are both odd positions and have the same value. Let's say he takes the left coin with value 1 because he built his algorithm to scan from left to right.
- Alice takes the left coin with value 3 (original position=1).
- Bob again can't take an odd position coin, but he takes the left coin with value 6 because it has a higher value than the right coin with value 1.
- Alice takes the left coin with value 3 (original position=3)
- Bob takes the last coin with value 1.
The result is the same as if Bob used the normal greedy approach, because Alice always take the coins away from Bob as she gets to go first.
Dynamic Programming Algorithm
We always assume that Bob will play optimally, optimally meaning that he will always take the coin which minimizes the total amount of coins that Alice can get.