Optimal Score Difference in an Endpoint Game
Problem statement
Two players alternately remove either the first or the last number from nums until the array is empty. A removed number is added to that player's score. The first player moves first.
Both players play optimally to maximize their own final score. Return the first player's score minus the second player's score. The answer may be negative.
Function
maximumScoreDifference(nums: int[]) → intExamples
Example 1
nums = [1,5,2]return = -2The first player can collect 3 while the second collects 5, giving -2.
Example 2
nums = [4,7,2,9]return = 10The first player can secure 16 while the second collects 6, giving 10.
Constraints
1 <= nums.length <= 1000.-10^6 <= nums[i] <= 10^6.