Problem
You are given an integer array nums
. Two players are playing a game with this array: player 1 and player 2.
Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0
. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0]
or nums[nums.length - 1]
) which reduces the size of the array by 1
. The player adds the chosen number to their score. The game ends when there are no more elements in the array.
Return true
if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true
. You may assume that both players are playing optimally.
Examples
Example 1:
|
|
Example 2:
|
|
Similar Problem
Maximum gain in a two-player game with picking elements from array ends
Solution
Method 1 - Recursion
Here is the approach:
- Base Condition: If there is only one element left in the array, Player 1 picks it, so we return that value.
- Recurrence Relation: For each subarray
nums[i...j]
, Player 1 can choose eithernums[i]
ornums[j]
. Player 1 then aims to maximize their net score (their score minus Player 2’s score) after Player 2’s optimal play on the remaining subarray.- If Player 1 picks
nums[i]
, Player 2 will play optimally onnums[i+1...j]
. - If Player 1 picks
nums[j]
, Player 2 will play optimally onnums[i...j-1]
. - Thus, the recursive formula is
max(nums[i] - helper(i+1, j), nums[j] - helper(i, j-1))
.
- If Player 1 picks
Code
|
|
|
|
Complexity
- Time:
O(2^n)
- Space:
O(n)
Method 2 - Top Down DP
Here is the memoized solution:
- Array Initialization: Create a memoization table
memo
to store intermediate results. - Base Condition: If there is only one element left in the array, Player 1 picks it, so we return that value.
- Recurrence Relation: For each subarray
nums[i...j]
, Player 1 can choose eithernums[i]
ornums[j]
. Player 1 then aims to maximize their net score (their score minus Player 2’s score) after Player 2’s optimal play on the remaining subarray.- If Player 1 picks
nums[i]
, Player 2 will play optimally onnums[i+1...j]
. - If Player 1 picks
nums[j]
, Player 2 will play optimally onnums[i...j-1]
. - Use the memoization table
memo
to store and retrieve previously computed results. - Thus, the recursive formula is
max(nums[i] - helper(i+1, j), nums[j] - helper(i, j-1))
.
- If Player 1 picks
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n^2)
, because we are filling up ann x n
memoization table - 🧺 Space complexity:
O(n^2)
, due to the memoization table used to store intermediate results and the recursion stack, which is bound byO(n)
, but typically the memoization table dominates.
Method Bottom up - TD DP
Here is the approach:
- Array Initialization: Create a 2D array
dp
wheredp[i][j]
represents the maximum score Player 1 can achieve over Player 2 from the subarraynums[i...j]
. - Base Condition: For subarrays of one element,
dp[i][i] = nums[i]
, since Player 1 takes the only available number. - Fill the DP Table: For each subarray
nums[i...j]
, Player 1 can choose eithernums[i]
ornums[j]
. Player 1 then aims to maximize their net score (their score minus Player 2’s score) after Player 2’s optimal play on the remaining subarray.- If Player 1 picks
nums[i]
, Player 2 will play optimally onnums[i+1...j]
. - If Player 1 picks
nums[j]
, Player 2 will play optimally onnums[i...j-1]
. - Thus,
dp[i][j] = max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1])
.
- If Player 1 picks
Code
|
|
|
|
Complexity
- Time:
O(n^2)
- Space:
O(n^2)