907. Sum of Subarray Minimums

Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Example 1:

Input: arr = [3,1,2,4]
Output: 17
Explanation: 
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
Sum is 17.

Example 2:

Input: arr = [11,81,94,43,3]
Output: 444

Constraints:

  • 1 <= arr.length <= 3 * 10^4
  • 1 <= arr[i] <= 3 * 10^4

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
    public int sumSubarrayMins(int[] arr) {
        // increasing stack
        Stack<Integer> prevLess = new Stack<>();
        Stack<Integer> nextLess = new Stack<>();
        
        int[] left = new int[arr.length];
        int[] right = new int[arr.length];
        
        for (int i = 0; i < arr.length; i++) {
            while (prevLess.size() > 0 && arr[i] <= arr[prevLess.peek()]) {
                prevLess.pop();
            }
            left[i] = prevLess.size() == 0 ? i + 1: i - prevLess.peek();
            prevLess.push(i);
        }
        
        for (int i = arr.length - 1; i >=0; i--) {
            while (nextLess.size() > 0 && arr[i] < arr[nextLess.peek()]) {
                nextLess.pop();
            }
            right[i] = nextLess.size() == 0 ? arr.length - i:  nextLess.peek() - i;
            nextLess.push(i);
        }
        long MOD = (long) 1e9 + 7;
        long res = 0;
        for (int i = 0; i < arr.length; i++) {
            res = (res + (long)arr[i] * left[i] * right[i]) % MOD;
        }
        return (int) res;
    }
}