Coins on the Table
Two players share the coins on a table. They take turns, the first player starts, and on a turn a player takes any one coin that is still there. Each of them plays to finish with as much money as possible, and the game ends when the table is empty.
Return how far ahead the first player finishes: the total of the first player minus the total of the second.
Example 1
coins [5,1,8]
returns 4
The first player takes 8, the second takes 5, and the first takes 1. That is 9 against 5, a lead of 4.
Example 2
coins [4,4]
returns 0
Each player gets one coin worth 4, so nobody is ahead.
Example 3
coins [7]
returns 7
The first player takes the only coin.
Constraints
0 <= len(coins) <= 301 <= coins[i] <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Greedy problems
- Fewest BanknotesBeginner
- Fewest JugsBeginner
- Greedy CoinsBeginner
- Least Total Waiting TimeBeginner
- Most Tasks in a Time BudgetBeginner
- Only the Best CountBeginner
- Shortest Walk to SeatsBeginner
- Stepping StonesBeginner