907 Sum of Subarray Minimums

1. Question

Given an array of integersA, find the sum ofmin(B), whereBranges over every (contiguous) subarray ofA.

Since the answer may be large,return the answer modulo10^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. 1 <= A.length <= 30000

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

2. Implementation

(1) Stack

思路:

3. Time & Space Complexity

(1) Stack: 时间复杂度, 空间复杂度

Last updated

Was this helpful?