Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions java/11-Container-With-Most-Water.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int res = 0;
while (left < right) {
int containerLength = right - left;
int area = containerLength * Math.min(height[left], height[right]);
res = Math.max(res, area);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return res;
}
}

/*
* Using two pointers approach. The container length is the right pointer
* subtract by the left pointer.
* Then we take the container length and multiply it by the minimum value of the
* height at left and the height at right, since the minimum value
* determines how much water we can hold. If the height at left is smaller than
* right, we move the left for a potential bigger height.
* If the left point and the right pointer have equal value, we can move either
* of them, it doesn't matter.
*
* Time: O(n)
* Space: O(1)
*/