Container With Most Water
Find two vertical lines that together with the x-axis form a container holding the maximum amount of water. The premier problem for learning two-pointer greedy bottleneck elimination.
Problem Statement
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Examples & Constraints
- n == height.length
- 2 <= n <= 10^5
- 0 <= height[i] <= 10^4
Two Pointers Greedy Inward Scan
Optimal ApproachStart with pointers at both extremes (maximum width). At each step, calculate area and move the pointer with the smaller height inward to potentially find a taller bottleneck in O(n) time.
Each iteration moves either the left or right pointer by one position inward. The loop runs at most N - 1 times.
Operates strictly in-place with two integer index variables and a running maximum variable.
Underlying algorithmic logic
The Mathematical Formula
The volume of water between two vertical lines at indices left and right is:area = (right - left) * min(height[left], height[right])
Here, (right - left) is the width, and the shorter line is the limiting bottleneck.
The Greedy Choice: Why Discard the Shorter Line?
Suppose height[left] < height[right]:
- If we keep
leftand moverightinward toright - 1, the width will decrease by 1. - What about the height? The new height is bounded by
min(height[left], height[right - 1]), which can never exceedheight[left]. - Decreased width equal or smaller height = strictly smaller area*!
Therefore, keeping the shorter line left while moving right inward can never yield a larger area than what we have already evaluated. We can safely discard left and advance left++!
By moving the pointer at the shorter line inward at every step:
- We start with the maximum possible width (
left = 0,right = n - 1). - We evaluate the current area and update
maxArea. - We advance the pointer with the smaller height.
- When
left >= right, we have proven that no container can exceedmaxArea.
Step-by-step execution walkthrough
left = 0 (height 1), right = 8 (height 7). Width = 8. Area = min(1, 7) * 8 = 8. maxArea = 8.
height[0] (1) < height[8] (7). Discard index 0. Advance left to 1 (height 8).
Width = 8 - 1 = 7. Area = min(8, 7) * 7 = 49. New maxArea = 49!
height[8] (7) < height[1] (8). Decrement right to 7 (height 3). Width continues shrinking.
- •Optimal O(n) single pass runtime
- •O(1) auxiliary space without mutating the input
- •Greedy elimination proof can be unintuitive to prove during interview without width/height bounding logic
Code Implementations
Ready-to-useCopy & paste production-ready solutions for Two Pointers Greedy Inward Scan in your language of choice
def max_area(height: list[int]) -> int:left = 0right = len(height) - 1max_water = 0while left < right:current_width = right - leftcurrent_height = min(height[left], height[right])current_water = current_width * current_heightif current_water > max_water:max_water = current_waterif height[left] < height[right]:left += 1else:right -= 1return max_waterif __name__ == "__main__":test1 = [1, 8, 6, 2, 5, 4, 8, 3, 7]test2 = [1, 1]print("test1 max area:", max_area(test1))print("test2 max area:", max_area(test2))
Understand Array & Matrix Visually
Explore sequential element access, subsegment manipulations, insertion, deletion, and 2D grid matrix walks. Test live edge cases and watch memory state transitions step-by-step.
Frequently Asked Questions
Related Logic & Algorithm Problems
3Sum
Find all unique triplets in an array that sum up to zero. The premier problem for learning how to combine sorting with two-pointer inward scans to avoid cubic brute force.
Valid Palindrome
Determine if a string is a palindrome considering only alphanumeric characters and ignoring case. The canonical problem for learning the two-pointer inward convergence technique.
Trapping Rain Water
Given an elevation map where width of each bar is 1, compute how much water it can trap after raining. The classic masterclass in two-pointer bottleneck derivation.