FastPrepMaximum Weighted End-Pick Score

Maximum Weighted End-Pick Score

Google logoGoogle● HardNEW GRADPHONE SCREEN
Learn

Problem statement

Choose exactly multipliers.length numbers from nums. At each step, remove either the first or last remaining number. Multiply the number chosen at zero-based step i by multipliers[i] and add it to the score.

Return the largest possible total score. The multiplier order is fixed, and every multiplier must be used, even when all possible totals are negative.

Function

maximumWeightedEndScore(nums: int[], multipliers: int[]) → long

Examples

Example 1

nums = [1,2,-3,4]multipliers = [1,1,-1,-1]return = 6

The source author gives picks 1, 4, -3, 2: their weighted total is 1 + 4 + 3 - 2 = 6.

Example 2

nums = [1,2,3]multipliers = [3,2]return = 13

Choose 3 and then 2 from the right: 3 * 3 + 2 * 2 = 13.

Example 3

nums = [-5,-2,-4]multipliers = [1,1]return = -6

Choose -4 and then -2; exactly two picks are required.

Constraints

  • 1 <= nums.length <= 100000.
  • 1 <= multipliers.length <= min(nums.length, 200).
  • Every number and multiplier lies between -10^6 and 10^6.
  • Return a signed 64-bit total.

More Google problems

See Google hiring insights
public long maximumWeightedEndScore(int[] nums, int[] multipliers) {
    // Write your code here
}
nums[1,2,-3,4]
multipliers[1,1,-1,-1]
expected6
Checking account…