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