Most water between two lines: why is moving the shorter line safe?
Question
You get a list of line heights. The lines stand one unit apart, in this order. Any two lines and the floor hold water. The width is the distance between the lines, and the water level is the height of the shorter line. Return the largest amount of water one pair can hold, in O(n) time. With fewer than two lines, return 0.
Then explain why it is safe to skip the shorter line of the pair you just measured.
max_water([4, 8, 3, 7, 2, 6]) # 24, lines 1 and 5 max_water([5]) # 0
def max_water(heights): ...
Show hints (3)
- "START WIDE"
Start with the two outer lines. They are the widest pair, so every other pair is narrower. What can you say about the other pairs that use the shorter of these two lines?
- "THE SHORTER LINE"
Say the left line is shorter. Pair it with any line between the two pointers. The width shrinks, and the water level can't rise above the left line. So none of those pairs beats the pair you just measured.
- "THE FULL PLAN"
Set
left = 0andright = len(heights) - 1. Measure the pair and updatebest. Then move the pointer at the shorter line one step inward. Stop when the pointers meet.
Show checked explanation
Start with the outer pair and measure it. Then move the pointer at the shorter line inward. Every other pair with that line is narrower, and its level can't rise above that line, so none of them beats the pair you just measured. Each step drops one line: O(n) time and O(1) memory.
def max_water(heights): left, right = 0, len(heights) - 1 best = 0 while left < right: level = min(heights[left], heights[right]) best = max(best, level * (right - left)) if heights[left] <= heights[right]: left += 1 else: right -= 1 return best print(max_water([4, 8, 3, 7, 2, 6])) print(max_water([5])) print(max_water([]))
24 0 0
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
Here is [4, 8, 3, 7, 2, 6]. Indexes start at 0.
| step | left | right | heights | water | move |
|---|---|---|---|---|---|
| 1 | 0 | 5 | 4 and 6 | 4 · 5 = 20 | 4 is shorter: left += 1 |
| 2 | 1 | 5 | 8 and 6 | 6 · 4 = 24 | 6 is shorter: right -= 1 |
| 3 | 1 | 4 | 8 and 2 | 2 · 3 = 6 | 2 is shorter: right -= 1 |
| 4 | 1 | 3 | 8 and 7 | 7 · 2 = 14 | 7 is shorter: right -= 1 |
| 5 | 1 | 2 | 8 and 3 | 3 · 1 = 3 | 3 is shorter: right -= 1 |
Now left == right, so the loop ends. The answer is 24, from lines 1 and 5.
Why may step 1 drop line 0 for good? The pair (0, 5) holds 20. Every other partner of line 0 is closer, so the width is at most 4. The water level can't go above line 0's height, 4. So any other pair with line 0 holds at most 4 · 4 = 16, less than 20.
Every step works the same way. The shorter line can't do better with any line inside the range, and its best pair is already in best. So it can go.
The loop measured only 5 of the 15 pairs. Every pair it skipped holds no more than a pair it measured. With n lines it measures n - 1 pairs.
Common mistakes
- Moving the taller line. On
[4, 8, 3, 7, 2, 6], the first step then drops the line of height 6, and the pair of lines 1 and 5 is never measured. You get 20 instead of 24. - Stopping when the water goes down. In the trace above it goes 24, then 6, then 14. A narrower pair can still hold more when both of its lines are taller.
- Measuring the width as
right - left + 1. Lines have no width, so lines 1 and 5 are 4 units apart, not 5. - Checking every pair with two loops. It is correct but O(n²): 15 pairs for 6 lines, and about 5 billion pairs for 100,000 lines.
Follow-up questions
- "What if both lines have the same height?" Move either one. If both have height
h, every other pair with one of them is narrower and its level is at mosth. So you can even move both pointers at once. - "Return the two lines, not just the amount." Save
leftandrightwhenever a pair beatsbest. Several pairs can hold the same maximum, so say which one you return. With>you keep the first such pair the loop measures. With>=you keep the last one. - "How is this different from trapping rain water?" Here only two lines hold the water, and the lines between them don't matter. In trapping rain water every bar is a wall. For each bar you add the water above it, set by the highest bars on its left and right.
What a strong answer covers
Explains why the shorter line can go: every remaining pair with it is narrower and no higher, so nothing better is lost. Traces a small example, handles equal heights and fewer than two lines, and gives O(n) time with O(1) memory.
Open in interactive practice deck →