-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWaterContainer.java
36 lines (26 loc) · 923 Bytes
/
WaterContainer.java
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
package two_pointers;
public class WaterContainer {
public int maxArea(int[] heights) {
int start = 0;
int end = heights.length - 1;
int maxVolume = 0;
while(start < end) {
int width = end - start;
int height = Math.min(heights[start], heights[end]);
int volume = height * width;
maxVolume = Math.max(maxVolume, volume);
if(heights[start] < heights[end]) {
start++;
} else {
end--;
}
}
return maxVolume;
}
public static void main(String[] args) {
WaterContainer container = new WaterContainer();
assert container.maxArea(new int[]{1,8,6,2,5,4,8,3,7}) == 49 : "Test case 1 failed";
assert container.maxArea(new int[]{1,1}) == 1 : "Test case 2 failed";
System.out.println("All test cases passed!");
}
}