Container With Most Water

Medium Two Pointers AI round

height is a row of upright lines standing on flat ground, one unit apart: the line at position i is height[i] units tall.

Any two of the lines, with the ground between them, make a container. It holds water up to the shorter of the two lines, so the water it holds is that shorter height times the distance between the two lines.

Return the most water that a container made of two of the lines can hold.

Example 1

Input: height = [4,8,3,6,2,7]
Output: 28

The lines at positions 1 and 5 are 8 and 7 tall and 4 apart. The water rises to 7, the shorter of them, so they hold 7 × 4 = 28. No other two lines hold more.

Example 2

Input: height = [1,5,4,3]
Output: 6

Example 3

Input: height = [6,1,9,10,1,5]
Output: 25

Constraints

  • 2 ≤ height.length ≤ 2 × 10⁵
  • 0 ≤ height[i] ≤ 10⁴

Watch the explanation

More Two Pointers

All Two Pointers problems