DSA sheet · Two Pointers
Two Pointers DSA Notebook
My study notes from the Two Pointers playlist. Each page is written so that a beginner (or me, after forgetting everything) can read it once and understand where each pointer starts, which pointer moves and why that move can't skip the answer, the Python code line by line, and a full dry run with the pointers drawn on the array.
The one idea behind every page
Checking every pair is O(n²). With two pointers you look at two positions at once and, at each step, throw away one of them for good because it can't be part of a better answer. Each pointer only ever moves forward, so the scan is O(n).
Three shapes: opposite ends moving inwards (sorted arrays, containers, palindromes), same direction (a slow writer and a fast reader), and expanding outwards from a center (palindromic substrings).
Three shapes: opposite ends moving inwards (sorted arrays, containers, palindromes), same direction (a slow writer and a fast reader), and expanding outwards from a center (palindromic substrings).
Every page follows the same order:
① question in simple words → ② constraints → ③ intuition → ④ building the logic from examples → ⑤ approach steps → ⑥ code → ⑦ line by line → ⑧ dry run with the pointers drawn → ⑨ complexity & remember
Start here
Opposite ends, moving inwards
- 23Sumsort + fix one
- 5Container With Most Watermove the shorter wall
- 6Trapping Rain Waterleft max · right max
- 7Valid Palindromeskip non-letters
- 8Valid Palindrome IIdelete one char
Same direction & three pointers
Expanding outwards from a center
Numbers are the order in the playlist. "Next →" at the top of each page follows that order.