-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxwater.java
More file actions
67 lines (62 loc) · 1.74 KB
/
maxwater.java
File metadata and controls
67 lines (62 loc) · 1.74 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// import java.util.*;
// class maxwater{
// public static void MaxWater(ArrayList<Integer> list){
// int max=0;
// int height,width;
// for(int i=0;i<list.size();i++){
// for(int j=i+1;j<list.size();j++){
// height=Math.min(list.get(i),list.get(j));
// width=j-i;
// int waterholded=height*width;
// max=Math.max(max,waterholded);
// }
// }
// System.out.println(max);
// }
// public static void main(String[] args) {
// ArrayList<Integer> list = new ArrayList<>();
// list.add(1);
// list.add(8);
// list.add(6);
// list.add(2);
// list.add(5);
// list.add(4);
// list.add(8);
// list.add(3);
// list.add(7);
// MaxWater(list);
// }
// }
import java.util.*;
class maxwater {
public static void MaxWater(ArrayList<Integer> list) {
int max = 0;
int left = 0;
int right = list.size() - 1;
while (left < right) {
int height = Math.min(list.get(left), list.get(right));
int width = right - left;
int waterholded = height * width;
max = Math.max(max, waterholded);
if (list.get(left) < list.get(right)) {
left++;
} else {
right--;
}
}
System.out.println(max);
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(8);
list.add(6);
list.add(2);
list.add(5);
list.add(4);
list.add(8);
list.add(3);
list.add(7);
MaxWater(list);
}
}