-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path11-container-most-water.java
More file actions
39 lines (30 loc) · 1.21 KB
/
11-container-most-water.java
File metadata and controls
39 lines (30 loc) · 1.21 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
class Solution {
public int maxArea(int[] height) {
int maxArea = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
int currentHeight = Math.min(height[left], height[right]);
int currentWidth = right - left;
maxArea = Math.max(maxArea, currentHeight * currentWidth);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println("Testing Container With Most Water:");
int[] h1 = {1, 8, 6, 2, 5, 4, 8, 3, 7};
System.out.println("Input: [1,8,6,2,5,4,8,3,7] -> Output: " + sol.maxArea(h1) + " (Expected: 49)");
int[] h2 = {1, 1};
System.out.println("Input: [1,1] -> Output: " + sol.maxArea(h2) + " (Expected: 1)");
int[] h3 = {4, 3, 2, 1, 4};
System.out.println("Input: [4,3,2,1,4] -> Output: " + sol.maxArea(h3) + " (Expected: 16)");
int[] h4 = {1, 2, 1};
System.out.println("Input: [1,2,1] -> Output: " + sol.maxArea(h4) + " (Expected: 2)");
}
}