Find two lines that form a container holding the most water.
This problem follows the Greedy Two Pointers pattern, commonly found in the Two Pointers category. Recognizing this pattern is key to solving it efficiently in an interview setting.
Start with widest container. Move the shorter line inward to potentially find taller ones.
Always move the shorter side — moving the taller side can never increase the area since the height is bounded by the shorter line.
left = 0, right = n - 1, maxArea = 0
while left < right:
area = min(height[left], height[right]) * (right - left)
maxArea = max(maxArea, area)
if height[left] < height[right]:
left++
else:
right--
return maxAreaPractice Container With Most Water and similar Two Pointers problems with flashcards. Build pattern recognition through active recall.
Practice this problem