Skip to content
BASA DSA

Container With Most Water

MediumTwo Pointers

Vertical lines have heights height[i] at horizontal coordinate i. Choose two lines forming the largest water container and return its area: their distance times the shorter height.

Example 1

Input
height = [3,1,4,2,5]
Output
12

Explanation

Indices 0 and 4 give width 4 and limiting height 3, hence area 12.

Example 2

Input
height = [0,0]
Output
0

Explanation

Both lines have height zero, so the area is zero.

Example 3

Input
height = [6,6]
Output
6

Explanation

Two height-six lines one unit apart hold area 6.

Constraints

  • 2 <= len(height) <= 100000
  • Heights are 0..10000

Function contractmaxAreaArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…