LeetCode #11 Medium

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.
Constraints
  • n == height.length
  • 2 <= n <= 10⁵
  • 0 <= height[i] <= 10⁴
arraytwo-pointersgreedy
Open on LeetCode ↗
02

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.
How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def maxArea(self, height):
▶3 left, right = 0, len(height) - 1
▶4 best = 0
▶5 while left < right:
▶6 water = min(height[left], height[right]) * (right - left)
▶7 best = max(best, water)
▶8 if height[left] < height[right]:
▶9 left += 1
▶10 else:
▶11 right -= 1
▶12 return best
lines108162235445863778LRpairs: row × column1234567801234567no pair measured yetstart with the widest pair
lines9
pairs36
The two outer lines are the widest container there is. Every other pair is narrower, so it can only win by holding water higher. The triangle on the right has one square per pair: row is the left line, column the right line.
lines108162235445863778LRpairs: row × column12345678012345671×8=88shorter linewidthwaterbest so farlines 0 and 8 hold 8; drop shorter line 0
water8
best8
ruled out7
Lines 0 and 8 are 8 apart and the shorter is 1 tall, so they hold 1 × 8 = 8, the best so far. The left line (1) is shorter. Any other partner is closer and the water still cannot rise above 1, so line 0 has had its best pair. That rules out 7 more pairs without measuring them.
lines108162235445863778LRpairs: row × column12345678012345677×7=4949shorter linewidthwaterbest so farlines 1 and 8 hold 49; drop shorter line 8
water49
best49
ruled out6
Lines 1 and 8 are 7 apart and the shorter is 7 tall, so they hold 7 × 7 = 49, the best so far. The right line (7) is shorter. Any other partner is closer and the water still cannot rise above 7, so line 8 has had its best pair. That rules out 6 more pairs without measuring them.
lines108162235445863778LRpairs: row × column12345678012345673×6=1849shorter linewidthwaterbest so farlines 1 and 7 hold 18; drop shorter line 7
water18
best49
ruled out5
Lines 1 and 7 are 6 apart and the shorter is 3 tall, so they hold 3 × 6 = 18, not more than the best of 49. The right line (3) is shorter. Any other partner is closer and the water still cannot rise above 3, so line 7 has had its best pair. That rules out 5 more pairs without measuring them.
lines108162235445863778LRpairs: row × column12345678012345678×5=4049shorter linewidthwaterbest so farlines 1 and 6 hold 40; equal → drop line 6
water40
best49
ruled out4
Lines 1 and 6 are 5 apart and the shorter is 8 tall, so they hold 8 × 5 = 40, not more than the best of 49. Both lines are 8 tall. A pair that beats this one needs both lines taller than 8, so neither line can be in it: drop the right one. That rules out 4 more pairs without measuring them.
lines108162235445863778LRpairs: row × column12345678012345674×4=1649shorter linewidthwaterbest so farlines 1 and 5 hold 16; drop shorter line 5
water16
best49
ruled out3
Lines 1 and 5 are 4 apart and the shorter is 4 tall, so they hold 4 × 4 = 16, not more than the best of 49. The right line (4) is shorter. Any other partner is closer and the water still cannot rise above 4, so line 5 has had its best pair. That rules out 3 more pairs without measuring them.
lines108162235445863778LRpairs: row × column12345678012345675×3=1549shorter linewidthwaterbest so farlines 1 and 4 hold 15; drop shorter line 4
water15
best49
ruled out2
Lines 1 and 4 are 3 apart and the shorter is 5 tall, so they hold 5 × 3 = 15, not more than the best of 49. The right line (5) is shorter. Any other partner is closer and the water still cannot rise above 5, so line 4 has had its best pair. That rules out 2 more pairs without measuring them.
lines108162235445863778LRpairs: row × column12345678012345672×2=449shorter linewidthwaterbest so farlines 1 and 3 hold 4; drop shorter line 3
water4
best49
ruled out1
Lines 1 and 3 are 2 apart and the shorter is 2 tall, so they hold 2 × 2 = 4, not more than the best of 49. The right line (2) is shorter. Any other partner is closer and the water still cannot rise above 2, so line 3 has had its best pair. That rules out 1 more pair without measuring them.
lines108162235445863778LRpairs: row × column12345678012345676×1=649shorter linewidthwaterbest so farlines 1 and 2 hold 6; drop shorter line 2
water6
best49
ruled out0
Lines 1 and 2 are 1 apart and the shorter is 6 tall, so they hold 6 × 1 = 6, not more than the best of 49. The right line (6) is shorter. Any other partner is closer and the water still cannot rise above 6, so line 2 has had its best pair.
lines108162235445863778pairs: row × column1234567801234567every pair is measured or ruled outanswer: 49
answer49
pairs measured8 of 36
The pointers have met. Only 8 of the 36 pairs were measured; every other square was ruled out by a line that had already had its best pair. The best container is lines 1 and 8, holding 49.
05

Common pitfalls

Moving the taller line

✗ Wrong
if height[left] < height[right]:
    right -= 1
else:
    left += 1
✓ Right
if height[left] < height[right]:
    left += 1
else:
    right -= 1

Keeping 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

✗ Wrong
water = min(height[left], height[right]) * (right - left + 1)
✓ Right
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.

06

Edge cases

Only two lines

The loop runs once, measures min(height[0], height[1]) * 1, and the pointers meet. That single pair is the answer.

Lines of height 0

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.

07

Complexity

Time
O(n)
Space
O(1)
Each step moves one pointer one place inward, so the loop runs n − 1 times. Only the two pointers and the best value are stored.
08

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.