Medium
Container With Most Water
👶 The 5-Year-Old Summary
Two vertical lines with water between them. Height is the shorter line, width is the distance. Max area = height × width!
🎮 Interactive Visualizer
heights = [1,8,6,2,5,4,8,3,7]
🧠The Brain Hack
Start with max width! Two pointers at ends, move toward center. Always move the shorter line (smaller height). Why? Width is shrinking, so we need to find a taller line to possibly increase area!
💻 The Clean Solution
def max_area(height):
left, right = 0, len(height) - 1
max_water = 0
while left < right:
# Calculate current area
width = right - left
h = min(height[left], height[right])
max_water = max(max_water, width * h)
# Move the shorter line
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_water
# [1,8,6,2,5,4,8,3,7] → 49 (between 8 and 7)
Time: O(n)
Space: O(1)