Skip to content

Container With Most Water

MediumTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

You are given a list height where height[i] is the height of a vertical wall at position i. Pick two walls; together with the ground they hold water up to the shorter wall. The amount of water is min(height[i], height[j]) * (j - i). Return the most water any pair can hold.

height = [1, 8, 6, 2, 5, 4, 8, 3, 7]   →  49   (walls at 1 and 8: min(8, 7) * 7)
height = [1, 1]                        →  1
height = [4, 3, 2, 1, 4]               →  16

Constraints

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