leetcode_907

Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarray of A.

Since the answer may be large, return the answer modulo 10^9 + 7.

Example 1:

Input: [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.

Note:

1 <= A.length <= 30000 1 <= A[i] <= 30000

Solutions

  1. mono stack

  2. Sum[i] represents the the sum of min of subarrays end with A[i](includeing A[i]).

class Solution {
public:
    int sumSubarrayMins(vector<int>& A) {
        stack<int> s;
        vector<int> sums(A.size());
        for (int i = 0; i < A.size(); i++) {
            // monotonically increasing stack
            while (!s.empty() && A[s.top()] > A[i])
                s.pop();
            // all numbers left are larger than self
            if (s.empty())
                sums[i] = (i + 1) * A[i];
            else                
                // counts of subarrays with A[s.top()] as the minimum number and end with A[i] as right most element
                // counts of subarrays with A[i] as the minimum number and end with A[i].
                // if i - s.top() != 1, these exlucded numbers must be larger than A[i].
                sums[i] = sums[s.top()] + (i - s.top()) * A[i];
            s.push(i);
        }

        const long MOD = 1e9 + 7;
        long res = 0;
        for (auto n : sums)
            res = (res + n) % MOD;

        return res;
    }
};

or

  1. monstack

  2. ?????, how ???

Last updated

Was this helpful?