-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerWithMostWater.java
More file actions
32 lines (29 loc) · 933 Bytes
/
Copy pathContainerWithMostWater.java
File metadata and controls
32 lines (29 loc) · 933 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
class Solution {
public int maxArea(int[] height) {
// Create two pointers
int left = 0;
int right = height.length - 1;
// Keep track of the max water
int max = 0;
// In a while loop decrease the smaller side of container until a bigger one is found
while (left < right) {
int valLeft = height[left];
int valRight = height[right];
int curContainer = (right - left) * Math.min(valLeft, valRight);
if (curContainer > max) {
max = curContainer;
}
if (valRight < valLeft) {
while (height[right] <= valRight && left < right) {
right--;
}
}
else {
while (height[left] <= valLeft && left < right) {
left++;
}
}
}
return max;
}
}