Consider a row of n coins of values v1 . . . vn, where n is even. We play a game against an opponent by alternating turns. In each turn, a player selects either the first or last coin from the row, removes it from the row permanently, and receives the value of the coin. Determine the maximum possible amount of money we can definitely win if we move first.
Note: The opponent is as clever as the user.
Let us understand the problem with few examples:
- 5, 3, 7, 10 : The user collects maximum value as 15(10 + 5)
- 8, 15, 3, 7 : The user collects maximum value as 22(7 + 15)
Does choosing the best at each move gives an optimal solution? No.
In the second example, this is how the game can be finished:
- …….User chooses 8.
…….Opponent chooses 15.
…….User chooses 7.
…….Opponent chooses 3.
Total value collected by user is 15(8 + 7)
- …….User chooses 7.
…….Opponent chooses 8.
…….User chooses 15.
…….Opponent chooses 3.
Total value collected by user is 22(7 + 15)
So if the user follows the second game state, the maximum value can be collected although the first move is not the best.
Approach: As both the players are equally strong, both will try to reduce the possibility of winning of each other. Now let’s see how the opponent can achieve this.
There are two choices:
- The user chooses the ‘ith’ coin with value ‘Vi’: The opponent either chooses (i+1)th coin or jth coin. The opponent intends to choose the coin which leaves the user with minimum value.
i.e. The user can collect the value Vi + min(F(i+2, j), F(i+1, j-1) ).
- The user chooses the ‘jth’ coin with value ‘Vj’: The opponent either chooses ‘ith’ coin or ‘(j-1)th’ coin. The opponent intends to choose the coin which leaves the user with minimum value, i.e. the user can collect the value Vj + min(F(i+1, j-1), F(i, j-2) ).
Following is the recursive solution that is based on the above two choices. We take a maximum of two choices.
F(i, j) represents the maximum value the user can collect from i'th coin to j'th coin. F(i, j) = Max(Vi + min(F(i+2, j), F(i+1, j-1) ), Vj + min(F(i+1, j-1), F(i, j-2) )) As user wants to maximise the number of coins. Base Cases F(i, j) = Vi If j == i F(i, j) = max(Vi, Vj) If j == i + 1
22 4 42
- Time Complexity: O(n2).
Use of a nested for loop brings the time complexity to n2.
- Auxiliary Space: O(n2).
As a 2-D table is used for storing states.
Note: The above solution can be optimized by using less number of comparisons for every choice. Please refer below.
Optimal Strategy for a Game | Set 2
Your thoughts on the strategy when the user wishes to only win instead of winning with the maximum value. Like the above problem, the number of coins is even.
Can the Greedy approach work quite well and give an optimal solution? Will your answer change if the number of coins is odd? Please see Coin game of two corners
This article is compiled by Aashish Barnwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Optimal Strategy for a Game | Set 3
- Optimal Strategy for a Game | Set 2
- Optimal strategy for a Game with modifications
- Optimal Strategy for a Game | Special Gold Coin
- Optimal Strategy for the Divisor game using Dynamic Programming
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Optimal Binary Search Tree | DP-24
- Optimal Substructure Property in Dynamic Programming | DP-2
- Find optimal weights which can be used to weigh all the weights in the range [1, X]
- Secretary Problem (A Optimal Stopping Problem)
- Implementation of Tic-Tac-Toe game
- Variation in Nim Game