Skip to content

Commit d90d050

Browse files
authored
Create 907 Sum of Subarray Minimums.cpp
1 parent 661a90d commit d90d050

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

907 Sum of Subarray Minimums.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int sumSubarrayMins(vector<int>& arr) {
4+
int MOD=1000000007;
5+
int n=arr.size();
6+
long result=0;
7+
8+
vector<int> stack;
9+
for (int i=0; i<=n; ++i) {
10+
while (!stack.empty() && (i==n || arr[stack.back()]>arr[i])) {
11+
int j=stack.back();
12+
stack.pop_back();
13+
int prev=stack.empty() ? -1:stack.back();
14+
result=(result+(long long)arr[j] * (j-prev) * (i-j)) % MOD;
15+
}
16+
stack.push_back(i);
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)