Container With Most Water
Medium · two pointers
The list height describes a row of vertical walls standing side by side, one unit apart: wall i is at position i and is height[i] tall.
Pick any two walls and imagine a tank whose sides are those walls and whose floor is the ground. The tank holds (distance between the walls) * (the shorter wall's height) units of water, since water spills over the lower side. Return the largest amount any pair of walls can hold.
You cannot tilt the tank, and the walls in between do not matter.
Examples
Example 1
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
The walls at positions 1 (height 8) and 8 (height 7) are 7 apart: 7 * min(8, 7) = 49.
Example 2
Input: height = [1, 1]
Output: 1
One tank, 1 apart, height 1.
Example 3
Input: height = [4, 3, 2, 1, 4]
Output: 16
The two outer walls are 4 apart and both 4 tall.
Constraints
- 2 <= len(height) <= 10^5
- 0 <= height[i] <= 10^4