-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrainwater.java
More file actions
29 lines (28 loc) · 858 Bytes
/
Copy pathrainwater.java
File metadata and controls
29 lines (28 loc) · 858 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
public class rainwater
{
public int trap(int[] height) {
int left = 0;
int right = height.length - 1;
int leftMax = height[left];
int rightMax = height[right];
int res = 0;
while(left < right){
if(leftMax< rightMax){
left++;
leftMax = Math.max(height[left], leftMax);
res += leftMax - height[left];
}
else
{
right--;
rightMax = Math.max(height[right], rightMax);
res += rightMax - height[right];
}
}
return res;
}
public static void main(String[] args) {
int arr[]={4,2,0,6,3,2,5};
TrappingRainwater(arr);
}
}