Predict the Winner
1 approach, code in every language
Problem description
You are given an integer array nums. Two players take turns, and Player 1 moves first. On each turn a player takes either the first or the last element of the remaining array and adds its value to their own running score; the chosen element is then removed.
When no elements remain, the game ends. Assuming both players play optimally to maximize their own score, return true if Player 1 can finish with a score greater than or equal to Player 2, and false otherwise. If the scores are tied, Player 1 is considered the winner.
Example 1
Input: nums = [1,5,2]
Output: false
Whether Player 1 first takes the 1 or the 2, Player 2 can respond so that Player 2 captures the 5, ending with a strictly higher total. Player 1 cannot force at least a tie.
Example 2
Input: nums = [1,5,233,7]
Output: true
Player 1 can secure the large 233 by playing the ends optimally, guaranteeing a final score that is at least as high as Player 2.
Constraints
1 <= nums.length <= 200 <= nums[i] <= 10^7
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Bundle Claim Duel is our own problem on the technique behind Predict the Winner: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free