Container with Most Water
- 1 min read
- LC-Medium
- Two Pointers
Solution
- At any point in time, the bottleneck is the smaller height, so increase the smaller height pointer first
| Time | Space | Explanation |
|---|
O(n) | O(1) | |
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
ans = 0
while l < r:
ans = max(ans, min(height[l], height[r]) * (r - l))
if height[l] < height[r]:
l += 1
else:
r -= 1
return ans