leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0907.cpp (603B)
0 class Solution { 1 static const int MOD = 1e9 + 7; 2 3 public: 4 int sumSubarrayMins(const vector<int> &arr) { 5 const int n = arr.size(); 6 stack<int> st; 7 8 long long res = 0; 9 for (int right = 0, mid, left; right <= n; right++) { 10 while (!st.empty() && (right == n || arr[st.top()] > arr[right])) { 11 mid = st.top(), st.pop(); 12 left = st.empty() ? -1 : st.top(); 13 res = (res + (long long)arr[mid] * (right - mid) * (mid - left)) % MOD; 14 } 15 st.push(right); 16 } 17 return res; 18 } 19 };