-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.cpp
More file actions
33 lines (30 loc) · 688 Bytes
/
Copy path42.cpp
File metadata and controls
33 lines (30 loc) · 688 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:
int trap(vector<int>& height) {
int n = height.size();
int res = 0;
vector<int> s;
for (int i = 0; i < height.size(); ++i) {
while (!s.empty() && height[i] > height[s.back()]) {
int top = s.back();
s.pop_back();
int top_left;
if (s.empty()) {
break;
}
top_left = s.back();
res += (i - top_left - 1) * (min(height[i], height[top_left]) - height[top]);
}
s.push_back(i);
}
return res;
}
};
int main() {
Solution s;
vector<int> intervals = {4,2,0,3,2,5};
cout << s.trap(intervals) << endl;
return 0;
}