Skip to content
Onsite

Container With Most Water

MediumP1Coding

height[i] is the height of a vertical line drawn at x = i. Pick two lines; together with the x-axis they form a container. Return the largest amount of water a container can hold. The water level is limited by the shorter line, and the container cannot be tilted.

Example 1

Input
height = [1,8,6,2,5,4,8,3,7]
Output
49
Why
Lines at index 1 (8) and index 8 (7): min height 7 times width 7.

Example 2

Input
height = [1,1]
Output
1

Example 3

Input
height = [4,3,2,1,4]
Output
16

Constraints

  • 2 <= height.length <= 10^5
  • 0 <= height[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 8.