-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trap Rain Water
40 lines (39 loc) · 895 Bytes
/
Trap Rain Water
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
class Solution {
public:
int trap(vector<int>& height) {
int N=height.size();
int water=0;
int leftMax=0;
int rightMax=0;
int low=0;
int high=N-1;
while(low<=high)
{
if(height[low]<height[high])
{
if(height[low]>leftMax)
{
leftMax=height[low];
}
else
{
water+=leftMax-height[low];
}
low++;
}
else
{
if(height[high]>rightMax)
{
rightMax=height[high];
}
else
{
water+=rightMax-height[high];
}
high--;
}
}
return water;
}
};