Container With Most Water
Move the pointer at the shorter line inward; the taller one can never improve the area alone.
Decision · step 2 of 10Two Pointer Problems: Container with most water
Lines 1 and 7 are 8 apart, so they hold min(1, 7) x 8 = 8. New best. 1 is the shorter line and caps the depth, so only moving it can help. Left moves up.
What you will see
Bars as heights; the area between the pointers is shaded; the shorter side moves.
Cost
| Best | O(n) |
|---|---|
| Average | O(n) |
| Worst | O(n) |
| Space | O(1) |
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.
Screen readers: Pointers announce their name and index; each step announces which pointer moved and why.
Reduced motion: Pointers appear at their new index with a brief emphasis instead of gliding.