-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path907.cpp
More file actions
33 lines (30 loc) · 667 Bytes
/
Copy path907.cpp
File metadata and controls
33 lines (30 loc) · 667 Bytes
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
#include "common.h"
using namespace std;
class Solution {
public:
const int mod = 1000000007;
int sumSubarrayMins(vector<int>& arr) {
arr.insert(arr.begin(), 0);
vector<uint64_t> rst(arr.size(), 0);
stack<int> s;
s.push(0);
for (int i = 0; i < arr.size(); ++i) {
while (!s.empty() && arr[s.top()] > arr[i]) {
s.pop();
}
rst[i] = rst[s.top()] + arr[i] * (i - s.top());
s.push(i);
}
uint64_t n = 0;
for (const auto& item : rst) {
n = (n + item) % mod;
}
return n;
}
};
int main() {
Solution s;
vector<int> v = {3, 1, 2, 4};
cout << s.sumSubarrayMins(v) << endl;
return 0;
}