FastPrepSum of Subarray Minimums

Sum of Subarray Minimums

NVIDIA logoNVIDIA● MediumINTERNOA

Problem statement

Given an integer array arr, return the sum of the minimum value in every nonempty contiguous subarray.

Return the result modulo 10^9 + 7. A subarray contains consecutive elements of the array.

Function

sumSubarrayMins(arr: int[]) → int

Examples

Example 1

arr = [3,1,2,4]return = 17

The minimums of the ten subarrays are 3, 1, 1, 1, 1, 1, 1, 2, 2 and 4. Their sum is 17.

Example 2

arr = [2,2]return = 6

The three nonempty subarrays are [2], [2] and [2,2]. Each has minimum 2, so the sum is 6.

Example 3

arr = [5]return = 5

There is one nonempty subarray, [5], whose minimum is 5.

Constraints

  • 1 <= arr.length <= 30000.
  • 1 <= arr[i] <= 30000.

More NVIDIA problems

See NVIDIA hiring insights
Saved
public int sumSubarrayMins(int[] arr) {
    // Write your code here.
}
arr[3,1,2,4]
expected17
Checking account…