Container With Most Water
Pick the two lines that hold the most water between them, moving the shorter of two pointers inward.
The problem
You get an array height where height[i] is the height of a vertical line standing at position i. Any two lines, together with the ground, form a container that holds min(height[i], height[j]) * (j - i) units of water. Return the largest amount that any pair of lines can hold.
Input height = [2, 7, 4, 1, 8, 3]
Output 21
The lines of height 7 and 8 stand 3 apart, so they hold min(7, 8) * 3 = 21.
Input height = [4, 4, 4, 4]
Output 12
With equal heights the two lines farthest apart win: 4 * 3 = 12.
Key constraint: There can be up to 100,000 lines, so checking every pair (about 5 billion) is too slow.
How to spot the pattern
- You choose a pair from an array, and the score depends on both their distance and the shorter value.
- Starting wide and moving inward lets you throw away one line at every step.
The idea
Start with the widest container, made by the first and last lines. Its water level is set by the shorter line, and any narrower container that keeps that shorter line can only hold less, because the width shrinks while the level can't rise above it. So the shorter line can be dropped for good: move its pointer inward and keep track of the best area you have seen.
Brute force: Compute the area for every pair of lines, which takes O(n^2) time.
Step by step
- 1Set
left = 0,right = n - 1andbest = 0. - 2Compute the area
min(height[left], height[right]) * (right - left)and updatebest. - 3Move the pointer at the shorter line inward (either one if the heights are equal).
- 4Repeat while
left < right, then returnbest.
See the pattern move
The two pointers pattern on a small example. Step through it, or press play.
Code
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
best = 0
while left < right:
area = min(height[left], height[right]) * (right - left)
best = max(best, area)
# the shorter line can't do better with any closer partner
if height[left] < height[right]:
left += 1
else:
right -= 1
return bestComplexity
Time
O(n)
Space
O(1)
Each step drops one line, so the loop runs n - 1 times.
Watch out for
- Moving the taller line inward: the width shrinks and the level is still capped by the shorter line, so that move can never find a bigger area.
- Mixing this up with Trapping Rain Water: here only the two chosen lines hold water, and the lines between them don't matter.
Follow-up questions
Why is it safe to drop the shorter line?
Say height[left] is the shorter one. Every other container that uses it pairs it with a line between the pointers, so it is narrower and its level is still at most height[left]. None of them can beat the current area, so dropping that line loses nothing.
What changes in Trapping Rain Water?
There every bar holds water above it, up to the smaller of the tallest bars on its left and on its right. Two pointers still work, but you track the highest bar seen from each side and add water bar by bar instead of comparing areas.
In Hinglish
Shuru mein width sabse zyada lo. Paani chhoti line tak hi rukta hai, isliye har baar chhoti line wala pointer andar khisakao.