Patterns
Pattern recognition (when stuck, what to try)
Skim the Signal column to find your situation, reach for the named Technique, then open that section on the Techniques page for the how-to. "Practiced" lists LC problems in problem_log.md that exercised it.
| Signal | Technique | Practiced |
|---|---|---|
| Sorted array → find pair/triplet/k-sum | Two pointers (or binary search) | |
| Search a sorted/monotonic space | Binary search | LC 374 |
| Count elements ≥ threshold in sorted array; "leftmost match" | Binary search / bisect_left | LC 2300 |
| Peak / local max in unsorted array, O(log n) | Binary search on slope | LC 162 |
| Min/max value such that a condition holds (capacity, speed) | Binary search on the answer | LC 875 |
| Unsorted → find pair summing to K | Hashing (map of complements) | |
| Substring/subarray with a constraint | Sliding window | LC 643, 1456, 1004, 1493 |
| Top-k / kth smallest or largest | Heap of size k | LC 215 |
| Min/max extraction + custom ops over a huge/infinite domain | Heap + set + frontier counter | LC 2336 |
| Score = sum(k from A) × extremum(k from B) | Heap (sort by B, size-k on A) | LC 2542 |
| Pick k from either end, cheapest | Heap (two heaps + two pointers) | LC 2462 |
| Median of a stream | Heap (two heaps, max+min) | |
| Linear recurrence f(n) from last k values | Dynamic programming | LC 1137, 746 |
| Pick-or-skip with a per-step constraint (no-adjacent, cooldown) | Dynamic programming | LC 198 |
| Count / weight paths in a grid | Dynamic programming (2D) | LC 62 |
| Compare / align / edit two sequences | Dynamic programming (2D) | LC 1143, 72 |
| Tiling / complete-vs-jagged boundary | Dynamic programming (state-augmented) | LC 790 |
| Sequential decision with state-dependent actions | Dynamic programming (state-augmented) | LC 714 |
| "How many ways to…" / count paths | Dynamic programming (count) | |
| Min/max steps to reach a state | BFS (unweighted) / Dijkstra / DP | |
| Reachability / connected-component traversal | Graph DFS/BFS | LC 841, 399 |
| Weighted-edge path value composes along edges | Graph DFS (carry value in the tuple) | LC 399 |
| Count connected components | Graph DFS/BFS seeds, or Union-Find | LC 547 |
| Incremental connectivity / equivalence classes | Union-Find | LC 547 |
| Cycle / connectivity / dependency ordering | Union-Find / DFS colors / topo sort | |
| All permutations / combinations / subsets | Backtracking | LC 17, 216 |
| Combinations (order-free, no duplicates) | Backtracking (start index) | LC 216 |
| Merge / overlap intervals | Intervals (sort by start, sweep) | |
| Remove fewest / min arrows to stab intervals | Intervals (greedy, sort by end) | LC 435, 452 |
| Locally-optimal choice each step | Greedy (prove via exchange argument) | LC 605, 334 |
| Palindrome / reverse-in-place / optimal pair | Two pointers (from ends) | LC 345, 11 |
| Remove / compact / overwrite in place | Two pointers (same direction) | LC 443, 283 |
| Match / merge two parallel sequences | Two pointers (one per sequence) | LC 392 |
| Midpoint / nth-from-end / cycle in linked list | Two pointers (slow/fast) | LC 2095, 2130 |
| Reverse a linked list / sub-chain | Linked-list 3-pointer reverse | LC 206, 2130 |
| "Cancel / match most recent" (parens, undo) | Stack | LC 2390 |
| Next-greater / next-smaller element | Monotonic stack | LC 739 |
| Streaming span / count of prior ≤ current | Monotonic stack | LC 901 |
| Frequency / anagram / repeating chars | Hashing (Counter / dict) | LC 1207, 1657 |
| Set difference / membership across collections | Hashing (set ops) | LC 2215 |
| Match identical sequences across collections | Hashing (hash a tuple) | LC 2352 |
| Divide into self-similar pieces | Recursion / divide & conquer | LC 1071 |
| Range sum / range update queries | Prefix sum (or Fenwick tree) | LC 238 |
| Repeated prefix matches in a set of words | Trie | LC 208 |
| Top-k lex-smallest words per prefix (autocomplete) | Trie + bounded DFS | LC 1268 |
| LRU / fixed-capacity cache | OrderedDict, or dict + DLL (see Data Structures) | |
| Sliding time window over events | Sliding window (deque) | LC 933 |
| Tree traversal (pre/in/post, depth, leaves) | Trees (DFS) | LC 104, 872 |
| Tree DFS carrying state down the path | Trees (DFS + param) | LC 1448 |
| Path-sum / ancestor-window on a tree | Trees (DFS + backtracking path) | LC 437 |
| Tree answer derivable bottom-up | Trees (bottom-up DFS) | LC 1372, 236 |
| LCA / lowest node satisfying P | Trees (DFS return found-node up) | LC 236 |
| Tree level-order / first-or-last per level | Trees (BFS) | LC 199 |
| Tree per-level aggregate | Trees (BFS level snapshot) | LC 1161 |
| Shortest path on unweighted graph/grid | BFS | LC 1926 |
| Search / insert / delete in a BST | Trees (BST walk) | LC 700 |
| Mutate a tree (delete / insert / trim) | Trees (recurse, return new subtree root) | LC 450 |
| Popcount for every number in 0..n | Bit manipulation (bit DP) | LC 338 |
| Every element appears twice except one | Bit manipulation (XOR fold) | LC 136 |
| Bit-by-bit decision across operands | Bit manipulation (casework) | LC 2275 |