Container With Most Water
Given an array height where height[i] is the height of a vertical line at position i, pick two lines that together with the x-axis form a container holding the most water. Return that maximum area, where area = (distance between the two lines) * min(height of the two lines).
Open official problem prompt ↗Find the two vertical lines that trap the greatest volume of water between them.
Two people hold the ends of a flexible trough as wide as the array; water spills over the shorter side, so the shorter holder steps inward hoping to find a taller wall worth the lost width.
- Input
- height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
- Output
- 49
- Why
- Lines at indices 1 and 8 (heights 8 and 7) give width 7 * min(8,7) = 7 * 7 = 49, the largest possible.
n == height.length2 <= n <= 10^50 <= height[i] <= 10^4