Container With Most Water

Two Pointers, problem 6 of 7

Container With Most Water

Medium

LC #11

greedyopposite ends

Not attempted yet

height[i] is the height of a vertical line at x = i. Choose two lines; together with the x-axis they form a container. It holds

min(height[i], height[j]) * (j - i)

units of water. Return the most water any container can hold. (The container can't be tilted.)

Example 1

Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Lines at x=1 (8) and x=8 (7):
min(8, 7) * 7 = 49

Example 2

Input: height = [1, 1]
Output: 1

Example 3

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

Constraints

  • 2 ≤ len(height) ≤ 10⁵
  • 0 ≤ height[i] ≤ 10⁵

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.