forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum-of-subarray-minimums.cpp
40 lines (36 loc) · 1.04 KB
/
sum-of-subarray-minimums.cpp
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
33
34
35
36
37
38
39
40
// Time: O(n)
// Space: O(n)
// Ascending stack solution
class Solution {
public:
int sumSubarrayMins(vector<int>& A) {
static const int M = 1e9 + 7;
vector<int> left(A.size());
stack<pair<int, int>> s1;
for (int i = 0; i < A.size(); ++i) {
int count = 1;
while (!s1.empty() && s1.top().first > A[i]) {
count += s1.top().second;
s1.pop();
}
s1.emplace(A[i], count);
left[i] = count;
}
vector<int> right(A.size());
stack<pair<int, int>> s2;
for (int i = A.size() - 1; i >= 0; --i) {
int count = 1;
while (!s2.empty() && s2.top().first >= A[i]) {
count += s2.top().second;
s2.pop();
}
s2.emplace(A[i], count);
right[i] = count;
}
int result = 0;
for (int i = 0; i < A.size(); ++i) {
result = (result + A[i] * left[i] * right[i]) % M;
}
return result;
}
};