AlgoScope

Container With Most Water

Move the pointer at the shorter line inward. The taller one can never improve the area alone.

Two pointers, algorithm, intermediate
Lines 1 and 7 hold 8. That is a new best. The left pointer moves to index 1.active 1-8108162235445863778leftright1 < 7
Lines 1 and 7 hold 8. That is a new best. The left pointer moves to index 1.active 1-8108162235445863778leftright1 < 7

Two Pointer Problems: Container with most water

Decision, step 2 of 10

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.

Cost

Best
O(n)
Average
O(n)
Worst
O(n)
Space
O(1)

Taught by the same lesson

Two Pointer Problems covers these too, in the same run.