Container With Most Water
Medium
LC #11
greedyopposite endsNot 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⁵