Hints — p27 Container With Most Water

Read one at a time.


Hint 1 — Start wide, narrow inward. Area depends on (min(h_lo, h_hi)) * (hi - lo). The widest pair starts at lo=0, hi=n-1. As you narrow, width strictly decreases — so the only way to find a bigger area is for min(h_lo, h_hi) to increase. Two-pointer.


Hint 2 — Move which pointer? You must move one. Moving the TALLER pointer keeps the container height capped by the SHORTER side (it can’t grow), and width shrinks — strict loss. So move the SHORTER pointer.


Hint 3 — Tie case. When h_lo == h_hi, moving either is correct (the proof goes through with ). By convention, move lo. Code: if h[lo] < h[hi]: lo += 1 else: hi -= 1.


Hint 4 — Prove the move rule. WLOG h_lo ≤ h_hi. For any hi' < hi: width (lo, hi') is smaller AND container height min(h_lo, h_{hi'}) ≤ h_lo. So area (lo, hi') ≤ h_lo * (hi - lo) = current area. Every pair (lo, *) is dominated, so eliminate them all by moving lo. Be ready to recite this in the interview.


Hint 5 — Width formula. Width is hi - lo, not hi - lo + 1. (Two adjacent lines at indices 0, 1 are 1 apart.) Easy off-by-one in interview.


If still stuck: look at max_area in solution.py — 10 lines, plus the deeper insight section in README.md for the full proof.