-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.cpp
More file actions
61 lines (56 loc) · 1.89 KB
/
TrappingRainWater.cpp
File metadata and controls
61 lines (56 loc) · 1.89 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public:
int trap(vector<int>& height) {
if (height.size() < 3) return 0;
int length = height.size(), left = 0, right = length - 1, leftMax = height[0], rightMax = height[right], volume = 0;
while (left < right)
{
if (leftMax < rightMax) {
volume += leftMax - height[left++];
leftMax = max(height[left], leftMax);
}
else {
volume += rightMax - height[right--];
rightMax = max(height[right], rightMax);
}
}
return volume;
}
int trap2(vector<int>& height) {
int l = 0, r = height.size() - 1, water = 0, minHeight = 0;
while (l < r) {
while (l < r && height[l] <= minHeight) {
water += minHeight - height[l++];
}
while (l < r && height[r] <= minHeight) {
water += minHeight - height[r--];
}
minHeight = min(height[l], height[r]);
}
return water;
}
// 0ms
int trap3(vector<int>& height) {
int res = 0;
for(int i = 0 ; i < height.size(); i++){
if(this->bars.empty() || height[this->bars.top()] >= height[i]){
bars.push(i);
}else{
while(!this->bars.empty() && height[this->bars.top()]<height[i]){
int currBar = height[this->bars.top()];
this->bars.pop();
if(this->bars.empty())
break;
int barH = min(height[this->bars.top()],height[i]) - currBar;
int barL = i - this->bars.top() - 1;
res += barH * barL;
// this->bars.pop();
}
this->bars.push(i);
}
}
return res;
}
private:
stack<int> bars;
};