THE BUILDING BLOCKS
A few patterns. A lot of possibilities.
Understand the idea first. The right solution starts to look familiar.
01Start here
Hash map lookup
Trade a little space for fast lookups. Great for counting, grouping, and finding pairs.
02Essential
Two pointers
Move inward from both ends, or move two pointers at different speeds.
03Essential
Binary search
Use a sorted range and halve the possibilities with each comparison.
04Popular
Sliding window
Maintain a useful slice of your input as its left and right boundaries move.
05Next step
Monotonic stack
Keep elements in order to find the next greater or smaller value in one pass.
06Essential
Depth-first search
Follow one branch at a time. Recursion makes the repeated work feel natural.