10 patterns
FundamentalsThe everyday tools. Most easy and medium problems use one of these.
- 01
Two Pointers
Walk in from both ends and let the data tell you which side to move.
Example
167. Two Sum II – Input Array Is Sorted
- 02
Sliding Window
Grow the window to the right, shrink it from the left, never look back.
Example
3. Longest Substring Without Repeating Characters
- 03
Binary Search
Halve the search space every step until only the answer is left.
Example
704. Binary Search
- 04
Hash Map
Remember what you have seen so the answer is one lookup away.
Example
1. Two Sum
- 05
BFS / DFS
Explore a grid or graph one neighbour at a time and mark where you have been.
Example
200. Number of Islands
AdvancedBuilt on the basics, for the medium-hard problems interviews love.
- 06
Dynamic Programming
Solve each small case once, then build the big answer from the small ones.
Example
198. House Robber
- 07
Backtracking
Try a choice, explore everything after it, then undo it and try the next.
Example
78. Subsets
- 08
Heap / Priority Queue
Keep only the k best so far; the weakest of them is always on top.
Example
215. Kth Largest Element in an Array
- 09
Monotonic Stack
Keep a stack in sorted order; whatever gets popped has just found its answer.
Example
739. Daily Temperatures
- 10
Union-Find
Every group has one leader; joining two groups means one leader follows the other.
Example
684. Redundant Connection