-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.cpp
More file actions
21 lines (21 loc) · 680 Bytes
/
Copy path42.cpp
File metadata and controls
21 lines (21 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// onepass.cpp
class Solution {
public:
int trap(vector<int>& height) {
int low = 0, high = height.size() - 1, sum = 0;
while (low < high) {
while (low < high && height[low] < height[low + 1])
low++;
while (low < high && height[high] < height[high - 1])
high--;
int v1 = height[low], v2 = height[high];
if (v1 < v2)
while (low < high && v1 >= height[low])
sum += v1 - height[low++];
else
while (low < high && v2 >= height[high])
sum += v2 - height[high--];
}
return sum;
}
};