Two Pointers & Sliding Window — Master Cheatsheet (Problems 101–160)
Advertisement
Two Pointers & Sliding Window — Complete! 🎉
60 problems covering every variant of the two-pointer and sliding window patterns. This cheatsheet summarizes all patterns for rapid review.
Pattern Templates
1. Fixed Window
def fixed_window(nums, k):
s = sum(nums[:k])
best = s
for i in range(k, len(nums)):
s += nums[i] - nums[i-k]
best = max(best, s)
return best
Use for: Max sum subarray of size k, Permutation in String, Vowels in Substring
2. Variable Window (Shrinkable)
def variable_window(nums, target):
left = best = 0; state = initial()
for right, val in enumerate(nums):
update(state, val) # expand
while not valid(state): # shrink
undo(state, nums[left]); left += 1
best = max(best, right - left + 1)
return best
Use for: Longest Without Repeating, Min Size Subarray Sum, Fruit Baskets
3. Two Pointer — Inward
def two_ptr_inward(arr):
l, r = 0, len(arr)-1
while l < r:
if arr[l] + arr[r] == target: return [l,r]
elif arr[l] + arr[r] < target: l += 1
else: r -= 1
Use for: Two Sum II, 3Sum, Container with Water, Valid Palindrome, Boats
4. Two Pointer — Same Direction (Write Ptr)
def write_ptr(nums):
w = 0
for r in range(len(nums)):
if keep(nums[r]):
nums[w] = nums[r]; w += 1
return w
Use for: Remove Element, Remove Duplicates, Move Zeroes
5. Exactly K = atMost(K) − atMost(K−1)
def exactly_k(nums, k):
return at_most(nums, k) - at_most(nums, k-1)
Use for: Count Nice Subarrays, Binary Subarrays with Sum, K Different Integers
6. Monotonic Deque
from collections import deque
def mono_deque_max(nums, k):
dq = deque() # indices, decreasing by value
res = []
for i, v in enumerate(nums):
while dq and dq[0] < i-k+1: dq.popleft()
while dq and nums[dq[-1]] < v: dq.pop()
dq.append(i)
if i >= k-1: res.append(nums[dq[0]])
return res
Use for: Sliding Window Maximum, Shortest Subarray Sum ≥ K
7. Complement Window (n-k middle)
def complement_window(nums, k):
# Find longest middle window to skip
# Answer = n - max_middle_window
Use for: Max Points from Cards, Min Operations to Reduce X
Problem Quick Reference
| Problem | # | Pattern | Time |
|---|---|---|---|
| Valid Palindrome | 101 | Inward Two Ptr | O(n) |
| Reverse String | 102 | Inward Swap | O(n) |
| Squares Sorted Array | 103 | Outward Two Ptr → reverse fill | O(n) |
| Remove Element | 104 | Write Ptr | O(n) |
| Remove Duplicates | 105 | Write Ptr | O(n) |
| Merge Sorted Array | 106 | 3-Ptr from End | O(m+n) |
| Is Subsequence | 107 | Greedy Two Ptr | O(n) |
| Max Consecutive Ones III | 108 | Variable Window | O(n) |
| Longest Substring No Repeat | 111 | HashMap Variable | O(n) |
| Longest Repeating Replacement | 112 | maxFreq Window | O(n) |
| Permutation in String | 113 | Fixed + Freq Map | O(n) |
| Fruit Into Baskets | 114 | K-Distinct Window | O(n) |
| 3Sum | 120 | Sort + Two Ptr | O(n²) |
| Container with Most Water | 122 | Greedy Inward | O(n) |
| Boats to Save People | 123 | Greedy Inward | O(n log n) |
| Subarray Product < K | 125 | Prod Window | O(n) |
| Max Points from Cards | 126 | Complement Window | O(n) |
| Count Nice Subarrays | 128 | atMost(k)-atMost(k-1) | O(n) |
| K Different Integers | 131 | exactly(k) | O(n) |
| Frequency of Max Freq | 134 | Sorted + Window | O(n log n) |
| Sliding Window Maximum | 152 | Mono Deque | O(n) |
| Minimum Window Substring | 151 | have/need Counter | O(n) |
| Shortest Subarray ≥ K | 153 | Deque + Prefix | O(n) |
| Trapping Rain Water | 159 | Two Ptr + max tracking | O(n) |
MAANG Priority
Google: Sliding Window Maximum, Minimum Window Substring, Subarrays K Different, Shortest Subarray Sum≥K, Max Consecutive Ones III, Permutation in String
Amazon: Two Sum II, Container Most Water, Min Window Substring, Boats to Save People
Meta/Facebook: Valid Palindrome, Valid Palindrome II, Trapping Rain Water, 3Sum
Microsoft: Longest Repeating Replacement, Sort Colors, Merge Sorted Array
Next Series
Advertisement