Predict the Winner

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, either 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.

Example 1
Inputnums = [1,5,2]
Outputfalse
Player 1 can only end with a score of 3 while player 2 gets 5, so player 1 cannot win.
Example 2
Inputnums = [1,5,233,7]
Outputtrue
Player 1 can choose 1 first and then always choose 233, ending with more score than player 2.

Constraints

  • 1 <= nums.length <= 20
  • 0 <= nums[i] <= 10^7

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate