Container With Most Water
Move the pointer at the shorter line inward. The taller one can never improve the area alone.
Two 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.
Cost
- Best
- O(n)
- Average
- O(n)
- Worst
- O(n)
- Space
- O(1)
Before this
Taught by the same lesson
Two Pointer Problems covers these too, in the same run.