Explain max-area two-pointer.
Skip to solutionKEEP THE
mediumDSA
How do you solve the container with most water problem?
453 views
01
Understand the problem
arraystwo-pointers
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Place pointers at both ends; the area is min(height[l], height[r]) * (r - l). Always move the shorter wall inward, since keeping it can never increase the area. This greedy two-pointer sweep is O(n) versus the O(n²) brute force.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Greedy two pointers
Run Playgrounddef max_area(height):
left, right = 0, len(height) - 1
best = 0
while left < right:
best = max(best, min(height[left], height[right]) * (right - left))
if height[left] < height[right]:
left += 1
else:
right -= 1
return best
# --- demo ---
print(max_area([1, 8, 6, 2, 5, 4, 8, 3, 7])) # 4905
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 74 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.