Sum of Subarray Minimums
Given an array arr, consider every contiguous subarray and take the minimum of each. Return the sum of all those minimums, modulo 10^9 + 7.
Open official problem prompt ↗Add up the minimum of every contiguous subarray without enumerating the quadratically many subarrays, returning the result modulo 10^9 + 7.
Rather than paying attention to each subarray, ask every element: 'For how many windows are you the shortest person in the room?' Multiply that count by your height and add up everyone's contribution.
- Input
- arr = [3, 1, 2, 4]
- Output
- 17
- Why
- The subarray minimums are 3,1,2,4,1,1,2,1,1,1 and they sum to 17.
1 <= arr.length <= 3 * 10^41 <= arr[i] <= 3 * 10^4