← Whiteboard questions

Container With Most Water

Pick two walls that hold the most water

DSA, Medium, Two Pointers

Which structure or algorithm, and what does it cost.

A list heights gives the heights of vertical walls standing in a row, one unit apart. Pick any two walls. Water between them rises to the shorter wall and spans the distance between their positions. The amount is that distance times the shorter height. Walls in between do not matter. Return the largest amount you can get. Return 0 if there are fewer than two walls.

Examples

Input heights = [3, 9, 2, 7, 4, 8]
Output 32
Why walls 1 and 5 are 4 apart and the shorter has height 8, so 4 * 8 = 32.
Input heights = [6, 6]
Output 6
Why the only pair is 1 apart with height 6.
Input heights = [5]
Output 0
Why one wall cannot hold water.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) extra memory.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

The amount depends on the distance and on the shorter wall. What changes for both when you pick walls far apart?

Hint 2 the approach

Start with the widest pair. Every narrower pair has a smaller distance, so it can win only with a taller shorter wall.

Hint 3 the key step

Keep a marker at each end and note the amount. Move the marker on the shorter wall inward. Repeat until the markers meet.

Walkthrough

Full walkthrough brute force to best

Brute force

Compute the amount for every pair of walls and keep the largest. This is simple and always right. It takes O(n^2) time and O(1) memory. With 100,000 walls that is about 5 billion pairs, which is too slow.

Best method

Start with the widest pair, the first and last walls, and note its amount. Then move the marker on the shorter wall one step inward, note the new amount, and repeat until the markers meet. The answer is the largest amount seen. This is correct because the shorter wall of the current pair can be dropped for good. Any pair that keeps this wall and moves the other marker inward is narrower, and its height is still limited by this wall, so its amount is no larger than the one you already noted. Moving the taller wall would throw away a pair that could still win. Each step drops one wall, so there are at most n - 1 steps. Use the same idea when a score is a distance times a limit: start wide and drop the limiting side.

Hand trace

Run on [3, 9, 2, 7, 4, 8]. The markers start at 0 and 5: the distance is 5 and the shorter wall is 3, so the amount is 15. Wall 0 is shorter, so the left marker moves to 1. Now the distance is 4 and the shorter wall is 8, so the amount is 32, the best so far. Wall 5 is shorter, so the right marker moves to 4. The distance is 3 and the shorter wall is 4, so the amount is 12. Wall 4 is shorter, so the right marker moves to 3. The amount is 2 * 7 = 14. Wall 3 is shorter, so the right marker moves to 2, and the amount is 2. The markers then meet, and the answer is 32.

Code

def most_water(heights):
    lo, hi = 0, len(heights) - 1
    best = 0
    while lo < hi:
        best = max(best, (hi - lo) * min(heights[lo], heights[hi]))
        if heights[lo] < heights[hi]:
            lo += 1
        else:
            hi -= 1
    return best

Time and memory

O(n) time, because each step moves one marker inward and the markers meet after at most n - 1 steps. O(1) extra memory, because only two positions and the best amount are kept.

Common mistakes

  • Moving the taller marker. The distance shrinks and the shorter wall stays, so the amount can only fall.
  • Using the taller wall as the height. Water spills over the shorter wall, so the shorter one sets the level.
  • Counting the walls between the two instead of the distance between their positions. Walls 1 and 5 are 4 apart, not 3.

Related problems

More Two Pointers problems
Container With Most Water: walkthrough and Python solution