Predict the Winner

Two players take turns removing a number from either end of nums, and add it to their score. Player 1 moves first and both play optimally. Return true if Player 1 can win; a tie also counts as a win for Player 1.

30:00

Predict the Winner
medium
Free written solution
Topics
Companies

Two players take turns removing a number from either end of nums, and add it to their score. Player 1 moves first and both play optimally. Return true if Player 1 can win; a tie also counts as a win for Player 1.

Example 1:
Input: {"nums":[1,5,2]}
Output: false
Constraints:
  • 1≤nums.length≤201 \leq \text{nums.length} \leq 20

  • 0≤nums[i]≤1070 \leq \text{nums}[i] \leq 10^7

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,5,2]}

dp[i][j] = how far ahead the player to move can finish on nums[i..j] (their total minus the other player's). Player 1 wins if dp[0][2] >= 0.

nums: the window [i..j] is the game still to be played

1

0

5

1

2

2

dp[i][j] (row i, column j): lead of the player to move on nums[i..j]

·

·

·

·

·

·

Variables
VariableValue
n3
DepthFunction Call
Stack empty
0/8