-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerwithMostWaterlinearTC.java
More file actions
41 lines (37 loc) · 1017 Bytes
/
ContainerwithMostWaterlinearTC.java
File metadata and controls
41 lines (37 loc) · 1017 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
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.ArrayList;
public class ContainerwithMostWaterlinearTC {
// 2 pointer approch
public static int storeWater(ArrayList<Integer> height){
int maxWater = 0;
int lp = 0;
int rp = height.size()-1;
while(lp < rp){
//calculate water area
int ht = Math.min(height.get(lp), height.get(rp));
int width = rp - lp;
int currWater = ht * width;
maxWater = Math.max(maxWater, currWater);
//update pointer
if(height.get(lp) < height.get(rp)){
lp++;
}
else{
rp--;
}
}
return maxWater;
}
public static void main(String[] args) {
ArrayList<Integer> height = new ArrayList<>();
height.add(1);
height.add(8);
height.add(6);
height.add(2);
height.add(5);
height.add(4);
height.add(8);
height.add(3);
height.add(7);
System.out.println(storeWater(height));
}
}