36 · DP Patterns

#486 Predict the Winner

Mediumsolve on LeetCode ↗

frequently reported atAmazonGoogle

the problem

In plain words

Two players share a row of numbers. Taking turns, starting with player 1, each player removes one number from either end of the row and adds it to their own score. Both play as well as possible. Return true if player 1 finishes with a score at least as high as player 2's.

in nums = [1,5,2]

out false

Whatever end player 1 takes, player 2 takes the 5: 3 against 5.

in nums = [1,5,233,7]

out true

Player 1 takes 1; whichever end player 2 takes, 233 is left for player 1.

  • 1 ≤ nums.length ≤ 20
  • 0 ≤ nums[i] ≤ 10⁷

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #312
      Burst Balloons
      Hardin Dynamic Programming
    2. #5
      Longest Palindromic Substring
      Mediumin Dynamic Programming