![]() |
VOOZH | about |
Problem statement: 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 a few examples:
1. 5, 3, 7, 10 : The user collects maximum value as 15(10 + 5)
2. 8, 15, 3, 7 : The user collects maximum value as 22(7 + 15)Does choosing the best at each move give an optimal solution?
No. In the second example, this is how the game can finish:1.
.......User chooses 8.
.......Opponent chooses 15.
.......User chooses 7.
.......Opponent chooses 3.
Total value collected by user is 15(8 + 7)
2.
.......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.
We have discussed an approach that makes 4 recursive calls. In this post, a new approach is discussed that makes two recursive calls.
There are two choices:
1. 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 + (Sum - Vi) - F(i+1, j, Sum - Vi) where Sum is sum of coins from index i to j. The expression can be simplified to Sum - F(i+1, j, Sum - Vi)
2. 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 + (Sum - Vj) - F(i, j-1, Sum - Vj) where Sum is sum of coins from index i to j. The expression can be simplified to Sum - F(i, j-1, Sum - Vj)
The 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.
arr[] represents the list of coins
F(i, j) = Max(Sum - F(i+1, j, Sum-arr[i]),
Sum - F(i, j-1, Sum-arr[j]))
Base Case
F(i, j) = max(arr[i], arr[j]) If j == i+1
Simple Recursive Solution :
22 4 42
Time complexity : O(2^n)
Space Complexity : O(n)
Memoization Based Solution :
22 4 42
Time Complexity : O(n^2)
Space Complexity : O(n^2)
Another Approach: Another idea to easily solve the problem is :
If we denote the coins collected by us as a positive score of an equivalent amount, whereas the coins removed by our opponent with a negative score of an equivalent amount, then the problem transforms to maximizing our score if we go first.
Let us denote dp[i][j] as the maximum score a player can get in the subarray [i . . . j], then
dp[i][j] = max(arr[i]-dp[i+1][j], arr[j]-dp[i][j-1])
This dynamic programming relation can be justified as mentioned below:
This relation holds because
- If we pick the leftmost element, then we would get a score equivalent to
arr[i] - the maximum amount our opponent can get from the subarray [(i+1) ... j].- Similarly picking the rightmost element will get us a score equivalent to
arr[j] - the maximum amount of score our opponent gets from the subarray [i ... (j-1)].This can be solved using the simple Dynamic Programming relation given above. The final answer would be contained in dp[0][n-1].
However, we still need to account for the impact of introducing the negative score.
Suppose dp[0][n-1] equals VAL, the sum of all the scores equals SUM, and the total score of our opponent equals OPP,
- Then according to the original problem we are supposed to calculate abs(OPP) + VAL since our opponent does not have any negative impact on our final answer according to the original problem statement.
- This value can be easily calculated as,
VAL + 2*abs(OPP) = SUM
(OPP removed by our opponent implies that we had gained OPP amount as well, hence the 2*abs(OPP))
=> VAL + abs(OPP) = (SUM + VAL)/2
The implementation of the above approach is given below.
22
Time Complexity : O(n^2)
Space Complexity : O(n^2),
Efficient approach : Space optimization
In previous approach the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
Implementation:
Output
22Time Complexity : O(n^2)
Space Complexity : O(n),
This approach is suggested by Ojassvi Kumar.