FastPrepOptimal Score Difference in an Endpoint Game

Optimal Score Difference in an Endpoint Game

Rippling logoRippling● MediumINTERNFULLTIMEOA
Learn

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[]) → int

Examples

Example 1

nums = [1,5,2]return = -2

The first player can collect 3 while the second collects 5, giving -2.

Example 2

nums = [4,7,2,9]return = 10

The first player can secure 16 while the second collects 6, giving 10.

Constraints

  • 1 <= nums.length <= 1000.
  • -10^6 <= nums[i] <= 10^6.

More Rippling problems

See Rippling hiring insights
public int maximumScoreDifference(int[] nums) {
    // write your code here
}
nums[1,5,2]
expected-2
Checking account…