Sum of Subarray Minimums
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[]) → intExamples
Example 1
arr = [3,1,2,4]return = 17The 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 = 6The three nonempty subarrays are [2], [2] and [2,2]. Each has minimum 2, so the sum is 6.
Example 3
arr = [5]return = 5There is one nonempty subarray, [5], whose minimum is 5.
Constraints
1 <= arr.length <= 30000.1 <= arr[i] <= 30000.