Container With Most Water
Container With Most Water is LeetCode 11 (Medium). height[i] is the height of a vertical line standing at position i. Pick two lines; together with the x-axis they form a container that holds min(height[i], height[j]) * (j - i) units of water. Return the most water any pair can hold. The container cannot be tilted, and the lines between the two you pick do not get in the way.
- Up to 10⁵ lines, so trying all of the roughly 5·10⁹ pairs is too slow.
- Heights go up to 10⁴, so the largest possible answer, about 10⁹, fits in a 32-bit int.
- n == height.length
- 2 <= n <= 10⁵
- 0 <= height[i] <= 10⁴
Intuition
Start with the two outer lines: the widest container there is. Every other pair is narrower, so it can only win by holding water higher.
Now look at the shorter of the two lines. Pair it with any line in between and the width shrinks, while the water level still cannot rise above that short line. So the pair just measured is the best the short line will ever do. Measure, then drop the shorter line by moving that pointer inward.
- Nothing is lost: each line you drop has already had its best pair counted.
- One pass: one line leaves at every step, so the pointers meet after n − 1 steps.
You want the best pair from an array, and the score is width times the smaller of two values. When one end of the current pair can be proven useless for every remaining pair, two pointers moving inward replace the O(n²) search.
Approach
Before reading on, measure the outer pair of [1, 8, 6, 2, 5, 4, 8, 3, 7]. Then ask: can the line of height 1 be part of any bigger container? Aim for O(n) time and O(1) space.
Start at both ends
Set left = 0 and right = n - 1. This is the widest pair. Every pair looked at later is narrower, so it only wins if both of its lines are taller than the shorter line here.
Measure the pair
The water is min(height[left], height[right]) * (right - left): the shorter line sets the level and the gap between the lines is the width. Keep the largest value in best.
Move the shorter line inward
If height[left] < height[right], do left += 1; otherwise right -= 1. Every pair the shorter line still has is narrower and cannot hold water above that line, so none of them beats the pair just measured. Stop when the pointers meet.
Equal heights
When both lines are the same height, moving either one is safe. A pair that beats this one needs both of its lines taller than this height, so it cannot use either line, and dropping one of them loses nothing.
Code in Python, C++ and Java
Common pitfalls
Moving the taller line
if height[left] < height[right]:
right -= 1
else:
left += 1if height[left] < height[right]:
left += 1
else:
right -= 1Keeping the shorter line caps every later pair at its height while the width keeps shrinking, and the tall line that could have made a big container is thrown away. On [1, 8, 6, 2, 5, 4, 8, 3, 7] this returns 8 instead of 49.
Counting lines instead of gaps for the width
water = min(height[left], height[right]) * (right - left + 1)
water = min(height[left], height[right]) * (right - left)
The width is the distance between the two lines, not the number of lines. Two neighbouring lines are 1 apart. On [1, 1] the wrong line returns 2 instead of 1.
Edge cases
The loop runs once, measures min(height[0], height[1]) * 1, and the pointers meet. That single pair is the answer.
A pair that includes a 0 holds nothing, and the 0 is always the shorter line, so it is dropped on the next move. If every line is 0, best stays 0.
Complexity
Container With Most Water FAQ
How can one pass be enough when there are n(n − 1)/2 pairs?
Lay the pairs out as a triangle: row i, column j. Measuring the pair at the two pointers and dropping the shorter line rules out the rest of that line's row or column in one move, since none of those pairs can beat the one just measured. After n − 1 moves every pair is either measured or ruled out. The demo below draws this triangle.
How is this different from Trapping Rain Water?
Here you pick one pair of lines, and the lines between them are thin and ignored. In Trapping Rain Water every bar is a solid block, and you add up the water sitting above each bar across the whole array.