Medium · Opposing Two Pointers
Understand → Visualise → Practice.
Choose two vertical lines and return the maximum water area between them.
The area is width × the shorter height. Moving the taller wall inwards can never help, because the width shrinks and the height stays limited by the shorter one, so always move the shorter wall.
Pointers at both ends give the maximum width.
Area = (right − left) × min(height[left], height[right]); keep the best.
Advance whichever pointer has the shorter line.
O(n) time and O(1) space.
Using the taller height instead of the shorter one overstates the container area.
Read the full guide first: Two Pointers and Sliding Windows.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.