-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_27.java
More file actions
45 lines (42 loc) · 1.05 KB
/
Copy pathDay_27.java
File metadata and controls
45 lines (42 loc) · 1.05 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
// Container With Most Water
class Solution {
public int maxArea(int[] height) {
int n = height.length;
int maxi = 0;
int l = 0, r = n - 1;
while(l<r){
int area = 1;
if(height[l]>=height[r]){
area = height[r] * (r-l);
r--;
}
else{
area = height[l] * (r-l);
l++;
}
maxi = Math.max(maxi,area);
}
return maxi;
}
}
// Trapping Rain Water
class Solution {
public int trap(int[] height) {
int n = height.length;
int l = 0, r = n - 1, lmax = 0, rmax = 0;
int total = 0;
while(l<r){
if(height[l]>=height[r]){
if(height[r]>rmax) rmax = height[r];
else total += (rmax - height[r]);
r--;
}
else{
if(height[l]>lmax) lmax = height[l];
else total+=(lmax-height[l]);
l++;
}
}
return total;
}
}