Two Pointers
01 / The shared mechanism
Two indices moving over the same sequence to maintain an invariant — the move rule changes per sub-pattern, but the mechanism is shared.
02 / The one-sentence essence
When you need a contiguous chunk that satisfies a condition, grow it from the right until it fails, then shrink from the left until it works again.
Problemlongest subarray with sum ≤ kInput[3, 1, 2, 1, 4, 2, 1, 5]k8
30
11
22
13
44
25
16
57
Ready. Press play to grow the window from the right.
step
0 / 27
window sum
0
current length
0
best so far
0
0 / 27
03 / The pattern signature
# two indices over one sequenceleft, right ← 0while right < n:// 1. grow windowinclude arr[right] in statewhile invariant violated:remove arr[left]; left++// 2. record current valid windowbest ← max(best, right − left + 1)right++
04 / When to recognize this pattern
"contiguous"
The answer is a slice of the array, not a subset. If you can reorder, this is the wrong tool.
"longest / shortest"
You're optimizing the length of that contiguous slice under a constraint.
"at most k"
A monotone constraint: if it holds for [l,r], a smaller window inside it also holds. This is the invariant the window rides on.
"k distinct"
Variants where the inner state is a multiset, but the mechanism is identical.
05 / Common pitfalls
Forgetting to shrink in a
while, not an if.A single shrink step rarely restores the invariant. The inner loop must run until valid — otherwise you pass on a broken window to the next step.
Recording the answer at the wrong moment.
Update
best after the shrink, when the window is valid — not during. The dirty intermediate state is not an answer.Off-by-one on window length.
Length is
right − left + 1 with inclusive bounds, or right − left with half-open. Pick one convention and never mix them.06 / Go practice — on LeetCode
easy3
medium29
01Longest Substring Without Repeating Characters— LC 3→02Longest Substring with At Most Two Distinct Characters— LC 159→03Minimum Size Subarray Sum— LC 209→04Longest Substring with At Most K Distinct Characters— LC 340→05Longest Substring with At Least K Repeating Characters— LC 395→06Longest Repeating Character Replacement— LC 424→07Find All Anagrams in a String— LC 438→08Max Consecutive Ones II— LC 487→09Subarray Sum Equals K— LC 560→10Permutation in String— LC 567→11Fruit Into Baskets— LC 904→12Binary Subarrays With Sum— LC 930→13Subarray Sums Divisible by K— LC 974→14Max Consecutive Ones III— LC 1004→15Grumpy Bookstore Owner— LC 1052→16Get Equal Substrings Within Budget— LC 1208→17Replace the Substring for Balanced String— LC 1234→18Count Number of Nice Subarrays— LC 1248→19Maximum Number of Occurrences of a Substring— LC 1297→20Number of Substrings Containing All Three Characters— LC 1358→21Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit— LC 1438→22Maximum Number of Vowels in a Substring of Given Length— LC 1456→23Longest Subarray of 1's After Deleting One Element— LC 1493→24Check If Array Pairs Are Divisible by k— LC 1497→25Maximize the Confusion of an Exam— LC 2024→26K Radius Subarray Averages— LC 2090→27Maximum Sum of Distinct Subarrays With Length K— LC 2461→28Take K of Each Character From Left and Right— LC 2516→29Maximum Sum of Almost Unique Subarray— LC 2841→
hard11
01Substring with Concatenation of All Words— LC 30→02Minimum Window Substring— LC 76→03Contains Duplicate III— LC 220→04Sliding Window Maximum— LC 239→05Sliding Window Median— LC 480→06Minimum Window Subsequence— LC 727→07Shortest Subarray with Sum at Least K— LC 862→08Subarrays with K Different Integers— LC 992→09Minimum Number of Operations to Make Array Continuous— LC 2009→10Maximum Number of Robots Within Budget— LC 2398→11Count Subarrays With Fixed Bounds— LC 2444→
— / When to use which
converging
Use when the array is sorted and the answer is a pair.
two-sum · palindrome · container
fast / slow
Use for in-place modification or cycle detection.
remove-dupes · linked-list cycles
sliding window
Use when the condition is about a contiguous chunk.
longest/shortest · at-most-k