Stone Game III

Hard

Topics
ArrayMathDynamic ProgrammingGame Theory

Given nums (the value of each stone in a row), Alice and Bob alternate turns with Alice first. On a turn a player takes the first 1, 2, or 3 remaining stones. Both play optimally to maximize their own total. Return Alice's score minus Bob's score (positive if Alice wins, negative if Bob wins, 0 for a tie).

Example 1

Input:  nums = [1,2,3,7]
Output: -4

Example 2

Input:  nums = [1,2,3,6]
Output: 0

Constraints

  • 1 <= nums.length <= 5*10^4
  • -1000 <= nums[i] <= 1000
Run ⌘' · Submit ⌘⏎