THE INSPECTION RAILTWO
TWO
POINTERS.
03PROBLEMS
Two positions. One reason to move.
Learn what you can safely leave behind.
00 / The idea before the code
Don’t ask “which pointer moves?” Ask “what can this move safely rule out?”
Start at two useful positions. Inspect what they tell you. Move a boundary only when you can explain why the discarded work cannot change the answer.
For a palindrome, matched edges are settled. For water, the shorter wall is a ceiling. For 3Sum, sorted order gives each move a direction.
STEP 1 OF 3Compare
Compare “a” with “a”, ignoring letter case.
LEFT0a
RIGHT3a
WHY THIS IS SAFEEverything outside these pointers has been verified. The inside is still undecided.
PAIRS0
COMPARISONmatch
YOUR TURN · PREDICT THE MOVE
These two characters match. What have we proved?
CHECK THE IDEA
The outside letters of “abca” match. Can we stop?
Three scenes. One pattern.
LEARN → PREDICT → RECALLGive movement a direction.
In sorted values, moving left forward makes its value larger or equal. Moving right backward makes its value smaller or equal.
−5−2035