通过这个题学习一下单调栈。
class Solution {
public:
int trap(vector<int>& height) {
stack<int> stk;
int res = 0, n=height.size();
for(int i=0; i<n; i++){
while(!stk.empty() && height[ stk.top() ] <= height[i]){
int midh = height[stk.top()];
stk.pop();
if(stk.empty()) continue;
int lh = height[stk.top()], w = i - stk.top() - 1;
res += (min(lh, height[i]) - midh)*w;
}
stk.push(i);
}
return res;
}
};