Skip to content
CodeShare
All problems

Container With Most Water

Pick two walls that hold the most water between them.

MediumArraysTwo pointersGreedy

You're given an array height where height[i] is the height of a vertical line at position i.

Choose two lines that, together with the x-axis, form a container. It holds (the distance between the lines) × (the height of the shorter line) units of water. Return the most water a container can hold.

Examples

Example 1
Input
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output
49
Why
The lines at positions 1 and 8 are 7 apart, and the shorter is 7 tall: 7 × 7 = 49.
Example 2
Input
height = [1, 1]
Output
1

Constraints

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