FastPrepMaximum Sum of Heights

Maximum Sum of Heights

Virtu Financial logoVirtu Financial● HardFULLTIMEOA
Learn

Problem statement

You are given positive maximum heights maxHeights. Choose a positive height for every index so that height[i] <= maxHeights[i].

The resulting array must be mountain-shaped: for some peak index, heights do not decrease before the peak and do not increase after it.

Return the maximum possible sum of the chosen heights.

Function

maximumSumOfHeights(maxHeights: int[]) → long

Examples

Example 1

maxHeights = [5,3,4,1,1]return = 13

Choosing [5,3,3,1,1] forms a mountain with sum 13.

Example 2

maxHeights = [6,5,3,9,2,7]return = 22

Using index 3 as the peak allows the mountain [3,3,3,9,2,2], whose sum is 22.

Constraints

  • 1 <= maxHeights.length <= 100000
  • 1 <= maxHeights[i] <= 10^9

More Virtu Financial problems

See Virtu Financial hiring insights
public long maximumSumOfHeights(int[] maxHeights) {
    // Write your code here.
}
maxHeights[5,3,4,1,1]
expected13
Checking account…