Asked at
Container With Most Water
MediumVerifiedArrayTwo PointersGreedy~20 min
Each height[i] is a vertical line at position i. Pick two lines that, with the x-axis, hold the most water and return that area.
Start with the widest pair and move the shorter line inward each step; widening can only help if the limiting height grows. The input arrives as a single array height.
Examples
in[1, 8, 6, 2, 5, 4, 8, 3, 7]
out49
Lines at indices 1 and 8 hold min(8,7)·7 = 49.
in[1, 1]
out1
min(1,1)·1 = 1.
Constraints
- 2 ≤ height.length ≤ 10⁵
- 0 ≤ height[i] ≤ 10⁴
- Target: O(n) time.
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.