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.

 

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 * 104
  • 1 <= arr[i] <= 3 * 104

Rust Solution

struct Solution;

impl Solution {
    fn sum_subarray_mins(a: Vec<i32>) -> i32 {
        let n = a.len();
        let mut sum = 0;
        let mut left: Vec<usize> = vec![0; n];
        let mut right: Vec<usize> = vec![0; n];
        let mut stack: Vec<usize> = vec![];
        for i in 0..n {
            left[i] = i + 1;
            right[i] = n - i;
            while let Some(j) = stack.pop() {
                if a[j] < a[i] {
                    stack.push(j);
                    break;
                } else {
                    right[j] = i - j;
                }
            }
            if let Some(&j) = stack.last() {
                left[i] = i - j;
            }
            stack.push(i);
        }
        for i in 0..n {
            sum += (left[i] * right[i]) as i32 * a[i];
            sum %= 1_000_000_007;
        }
        sum
    }
}

#[test]
fn test() {
    let a = vec![3, 1, 2, 4];
    let res = 17;
    assert_eq!(Solution::sum_subarray_mins(a), res);
}

Having problems with this solution? Click here to submit an issue on github.