Predict the Winner

Hard

Topics
ArrayMathDynamic ProgrammingGame Theory

Given a score array nums, two players take turns picking a number from either end; the picked value is added to that player's score. Player 1 moves first. Both play optimally. Return true if player 1 can end with a score greater than or equal to player 2.

Example 1

105122
Input:  nums = [1,5,2]
Output: false

Example 2

1051233273
Input:  nums = [1,5,233,7]
Output: true

Constraints

  • 1 <= nums.length <= 20
  • 0 <= nums[i] <= 10^7
Editorial — intuition, approach, complexity and pitfalls

Intuition

Tracking both players' scores doubles the state for no reason. Track the difference instead: dp[i][j] is the best (current player's score minus the opponent's) achievable on the window nums[i..j]. Because the players alternate, the opponent's best play on the remaining window is itself a dp value — from their point of view, with the sign flipped.

That gives the recurrence dp[i][j] = max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1]). Taking the left end scores nums[i] and then concedes dp[i+1][j] to the opponent; the subtraction is the turn change. Player 1 wins or ties exactly when dp[0][n-1] >= 0.

The sign flip is the whole idea, and it is what makes this O(n²) rather than a search over both scores.

Approach

  1. Seed dp[i][i] = nums[i]: with one number left, the current player takes it.
  2. Fill by increasing window length.
  3. For each window, take the better of the two ends, subtracting the opponent's best on what remains.
  4. Return whether dp[0][n-1] >= 0.

Dry Run

nums = [1,5,2]

  dp[i][i] = nums[i]  ->  dp[0][0]=1, dp[1][1]=5, dp[2][2]=2

  window  take left            take right           dp
  [0,1]   1 - dp[1][1] = 1-5 = -4   5 - dp[0][0] = 5-1 = 4    4
  [1,2]   5 - dp[2][2] = 5-2 =  3   2 - dp[1][1] = 2-5 = -3   3
  [0,2]   1 - dp[1][2] = 1-3 = -2   2 - dp[0][1] = 2-4 = -2  -2

dp[0][2] = -2, which is below 0

answer false

player 1 cannot win: taking either end hands player 2 the 5.

Complexity

  • Time — O(n²): one entry per window, constant work each.
  • Space — O(n²) for the table, reducible to O(n) by filling a single row in place by increasing length.

Common Pitfalls

  • Tracking both scores separately, which needs a far bigger state and obscures the alternation. The difference formulation is the point.
  • Forgetting the sign flip and writing nums[i] + dp[i+1][j]. That models both players cooperating.
  • Filling the table by row or column rather than by window length. dp[i][j] depends on dp[i+1][j] and dp[i][j-1], both shorter, so length order is the one that satisfies the dependencies.
  • Testing > 0. A tie counts as a win for player 1 here, so the comparison is >= 0.
  • Assuming a greedy "take the larger end" works. On [1, 5, 2] greedy takes the 2, then the opponent takes the 5 — and the DP shows no first move wins, which greedy cannot tell you.
  • Reading "both play optimally" as "both maximise their own score independently". Maximising your score is the same as maximising the difference only because the total is fixed — worth saying, since it is what licenses the single-value state.

Full Solution — all languages

class Solution:
    def predictTheWinner(self, nums):
        n = len(nums)
        if n == 0:
            return True
        dp = [[0] * n for _ in range(n)]
        for i in range(n):
            dp[i][i] = nums[i]
        for length in range(2, n + 1):
            for i in range(0, n - length + 1):
                j = i + length - 1
                dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1])
        return dp[0][n - 1] >= 0
SensAI
Beta
Tap the mic to stop voice mode