Choose two vertical walls that can hold the greatest rectangular area of water between them.
Problem
Input. A list heights of nonnegative integers; index i places a vertical wall at horizontal coordinate i.
Output. Return the largest container area formed by two distinct walls and the horizontal ground.
Rules. Walls cannot tilt, so the shorter wall limits the water height. Positions are unit-spaced; area is their separation multiplied by the smaller height. Preserve the input and its order. Fewer than two walls return zero.
Example. [1, 8, 6, 2, 5, 4, 8, 3, 7] returns 49.
Walls at indices 1 and 8 have width 7 and limiting height 7, giving area 7 * 7.
Picture: Evaluate Before Discarding a Wall
Problem trace
Separate full input: evaluate the walls before moving the shorter one
- covered range
- [0..8]
- best
- max(0, 8) = 8
- move
- L: 0 -> 1
- covered range
- [0..8]→[1..8]
- best
- max(0, 8) = 8→max(8, 49) = 49
- move
- L: 0 -> 1→R: 8 -> 7
- covered range
- [1..8]→[1..7]
- best
- max(8, 49) = 49→max(49, 18) = 49
- move
- R: 8 -> 7→R: 7 -> 6
- covered range
- [1..7]→[1..6]
- best
- max(49, 18) = 49→max(49, 40) = 49
- move
- R: 7 -> 6→R: 6 -> 5
- covered range
- [1..6]→[1..5]
- best
- max(49, 40) = 49→max(49, 16) = 49
- move
- R: 6 -> 5→R: 5 -> 4
- covered range
- [1..5]→[1..4]
- best
- max(49, 16) = 49→max(49, 15) = 49
- move
- R: 5 -> 4→R: 4 -> 3
- covered range
- [1..4]→[1..3]
- best
- max(49, 15) = 49→max(49, 4) = 49
- move
- R: 4 -> 3→R: 3 -> 2
- covered range
- [1..3]→[1..2]
- best
- max(49, 4) = 49→max(49, 6) = 49
- move
- R: 3 -> 2→R: 2 -> 1; pointers meet
- Recognize it
- Choose two boundary positions to maximize width times the smaller boundary height; moving inward always loses width, so only a taller limiting wall can compensate.
- Keep true
- Before each iteration, best is the largest area among discarded boundary pairs. Moving the shorter wall cannot discard a better pair because every pair that keeps it has smaller width and height no greater than that same short wall.
- Reuse it
- When moving either boundary worsens one factor, move the boundary responsible for the current bottleneck; retaining that bottleneck cannot improve the objective as the other factor shrinks.
Reference drawing
Problem trace
Moving only the taller wall cannot improve the shorter-wall limit
Execution trace
- covered range
- [0..8]
- best
- max(0, 8) = 8
- move
- L: 0 -> 1
- covered range
- [0..8]→[1..8]
- best
- max(0, 8) = 8→max(8, 49) = 49
- move
- L: 0 -> 1→R: 8 -> 7
- covered range
- [1..8]→[1..7]
- best
- max(8, 49) = 49→max(49, 18) = 49
- move
- R: 8 -> 7→R: 7 -> 6
- covered range
- [1..7]→[1..6]
- best
- max(49, 18) = 49→max(49, 40) = 49
- move
- R: 7 -> 6→R: 6 -> 5
- covered range
- [1..6]→[1..5]
- best
- max(49, 40) = 49→max(49, 16) = 49
- move
- R: 6 -> 5→R: 5 -> 4
- covered range
- [1..5]→[1..4]
- best
- max(49, 16) = 49→max(49, 15) = 49
- move
- R: 5 -> 4→R: 4 -> 3
- covered range
- [1..4]→[1..3]
- best
- max(49, 15) = 49→max(49, 4) = 49
- move
- R: 4 -> 3→R: 3 -> 2
- covered range
- [1..3]→[1..2]
- best
- max(49, 4) = 49→max(49, 6) = 49
- move
- R: 3 -> 2→R: 2 -> 1; pointers meet
- Recognize it
- Choose two boundary positions to maximize width times the smaller boundary height; moving inward always loses width, so only a taller limiting wall can compensate.
- Keep true
- Before each iteration, best is the largest area among discarded boundary pairs. Moving the shorter wall cannot discard a better pair because every pair that keeps it has smaller width and height no greater than that same short wall.
- Reuse it
- When moving either boundary worsens one factor, move the boundary responsible for the current bottleneck; retaining that bottleneck cannot improve the objective as the other factor shrinks.
Bars show heights; small numbers show indices. The bar’s L=1 denotes left height 1, while code starts at left=0.
| Index pair | Area | Move |
|---|---|---|
| 0, 8 | 8 | Discard left height 1 |
| 1, 8 | 49 | Discard right height 7 |
| 1, 7 | 18 | Discard right height 3 |
| 1, 6 | 40 | Equal heights: reference discards right |
| 1, 5; 1, 4; 1, 3; 1, 2 | 16; 15; 4; 6 | Keep best 49 |
If , any inner partner satisfies
.
Nonnegative height makes the width comparison safe. Evaluate this bound before deleting left; the taller-wall case is symmetric.
With equal heights, either deletion preserves the maximum value, not every tying pair.
Code
def max_area(heights: list[int]) -> int:
left, right = 0, len(heights) - 1
best = 0
while left < right:
height = min(heights[left], heights[right])
best = max(best, height * (right - left))
if heights[left] < heights[right]:
left += 1
else:
right -= 1
return best
Cost: time and space.
Change 1: Irregular Coordinates
New task. Walls stand at strictly increasing coordinates, supplied alongside equal-length nonnegative heights.
Picture and change. Replace index distance with coordinate distance. Moving inward still reduces width.
Code.
def max_area_positions(positions, heights):
left, right = 0, len(heights) - 1
best = 0
while left < right:
width = positions[right] - positions[left]
best = max(best, width * min(heights[left], heights[right]))
if heights[left] < heights[right]:
left += 1
else:
right -= 1
return best
Check. Coordinates [0, 2, 5], heights [3, 1, 3], give 15, not index-based area 6.
Use when. Spacing changes but coordinate order remains fixed.
Change 2: Return One Maximizing Pair
New task. Return an index witness as well as area.
Picture and change. Save each strict improvement. Initialize below zero to record a real zero-area pair.
Code.
def max_area_witness(heights):
left, right = 0, len(heights) - 1
best, pair = -1, None
while left < right:
area = (right - left) * min(heights[left], heights[right])
if area > best:
best, pair = area, [left, right]
if heights[left] < heights[right]:
left += 1
else:
right -= 1
return {"area": max(best, 0), "indices": pair}
Check. Main input gives area 49, indices [1, 8]; [0, 0] gives area 0, indices [0, 1].
Empty input has area 0 and no witness.
Use when. One certificate suffices; do not promise all ties.
Failure: Moving the Taller Wall
On [1, 2, 4, 3], that rule finds 3 but misses area 4 from indices 1 and 3.
Transfer Check
Does the proof survive rewarding the taller wall instead?
Reasoned Answer
No: the discarded-pair bound used the shorter wall. A changed objective needs a new bound.
What the Invariant Guarantees
best covers evaluated pairs and bounds every discarded pair. All potentially better pairs remain between the boundaries.
Each move removes one wall; when pointers meet, no unexamined pair can improve best.
This proves the maximum value, not complete enumeration of maximizing indices.
All three implementations use arithmetic operations and constant auxiliary state under ordinary unit-cost arithmetic. The coordinate variant relies on its stated order/length preconditions; it does not validate arbitrary input. The witness uses constant extra storage and does not change input heights.
Sorting [1, 100, 1, 100] changes the answer from 200 to 100 by moving walls.
Compare 3Sum: its deletion proof uses sorted values, while this proof uses fixed geometry.