Container With Most Water
The problem
Section titled “The problem”You get a list of heights, one vertical line per index. Choose any two lines. Together with the x-axis they form a container. Its water is the distance between the lines times the shorter of the two heights. Return the biggest amount possible.
height = [1, 8, 6, 2, 5, 4, 8, 3, 7] -> 49Lines at indices 1 and 8 (heights 8 and 7): min(8, 7) * (8 - 1) = 49.
Key insight
Section titled “Key insight”Brute force tries every pair: O(n²). Start instead with the widest container, the two ends, and ask how to find something better.
Moving a pointer inward makes the width smaller. So the area can only grow if the shorter wall gets taller. Every other container that keeps the shorter wall is narrower and still capped by that same short height, so none of them can beat the current area. So the shorter wall’s pointer can be discarded safely. Move it inward, track the best area, and repeat until the pointers meet.
Show solutionHide solution
def max_area(height: list[int]) -> int: left, right = 0, len(height) - 1 best = 0 while left < right: best = max(best, min(height[left], height[right]) * (right - left)) # The shorter wall limits the area, so only moving it can help. if height[left] < height[right]: left += 1 else: right -= 1 return bestexport function maxArea(height: number[]): number { let left = 0; let right = height.length - 1; let best = 0; while (left < right) { best = Math.max(best, Math.min(height[left], height[right]) * (right - left)); // The shorter wall limits the area, so only moving it can help. if (height[left] < height[right]) left++; else right--; } return best;}class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; int best = 0; while (left < right) { best = Math.max(best, Math.min(height[left], height[right]) * (right - left)); // The shorter wall limits the area, so only moving it can help. if (height[left] < height[right]) left++; else right--; } return best; }}Complexity
Section titled “Complexity”- Time: O(n) — every step moves one pointer inward, so there are n - 1 steps at most.
- Space: O(1) — two indices and a running best.
Watch out for
Section titled “Watch out for”- Move the shorter side, not the taller. Moving the taller one can only lose width without raising the height limit.
- Width is
right - left, not the count of lines. Heights are the lines; the gap between indices is the width. - Ties are fine. When both walls are equal height, moving either pointer is safe.
Variations
Section titled “Variations”Trapping Rain Water (LeetCode 42)
Section titled “Trapping Rain Water (LeetCode 42)”The next problem in this guide: water held between bars rather than between two chosen lines. The trick: also two pointers, but you sum water per position instead of maximizing one area.
Maximum Score of a Good Subarray (LeetCode 1793)
Section titled “Maximum Score of a Good Subarray (LeetCode 1793)”Choose a subarray containing index k, scored as min(nums[i..j]) * (j - i + 1).
The trick: start with both pointers at k and expand outward, always stepping
toward the larger neighbor so the minimum drops as slowly as possible. O(n).
Two Sum II - Input Array Is Sorted (LeetCode 167)
Section titled “Two Sum II - Input Array Is Sorted (LeetCode 167)”Find a pair in a sorted array that adds to a target. The trick: the same opposite-ends pointers. Too small, move left up; too big, move right down. Sorted order means the discarded side can never be part of the answer.