23 patterns.
Step by step.
Each pattern runs on a small example: pseudocode with the current line lit up, the variables, and a picture of the data. Then practise it on real LeetCode problems.
Arrays and strings11
P01Hash mapKnow at once if you have seen it before13 problems · Find a pair / CountingP02Two pointersPairs in sorted data14 problems · Towards each other / Same direction / Two arraysP03Sliding windowThe longest or shortest stretch that follows a rule8 problems · Variable size / Fixed sizeP04Prefix sumsMany sums of stretches of one array5 problems · Subarray sum / From both sidesP05Binary searchSearching sorted data12 problems · Find a number / Search the answer / Rotated arrayP06IntervalsRanges that overlap6 problems · Merge / Sort by endP07StackBrackets, nesting, undo5 problems · Brackets / EvaluateP08Monotonic stackThe next greater or smaller element5 problemsP09Heap and top KThe k largest, smallest or most frequent8 problems · k largest / Merge k lists / Two heapsP10Matrix traversalWalking and rotating a grid4 problems · Spiral / RotateP11Bit manipulationWhen each bit of a number matters6 problems · XOR / Bit by bit
Linked lists02
Trees and graphs06
P14Depth-first search (DFS)Islands, paths, trees15 problems · Tree nodes / Top-down / Graph / GridP15Breadth-first search (BFS)Shortest paths, tree levels11 problems · Tree nodes / Graph / Grid / StatesP16Tree traversalsVisit every node of a tree in the right order9 problems · In-order / Pre-order / Post-orderP17Trie (prefix tree)Autocomplete and prefix search4 problems · Insert and search / Search with .P18Topological sortOrder tasks that depend on each other4 problemsP19Union-FindWho is connected as links are added5 problems
Choices and optimisation04
P20BacktrackingAll subsets, permutations, combinations10 problems · Subsets / Permutations / Path on a gridP21Dynamic programming (DP)Best value or number of ways18 problems · 1D array / 2D table / Best subarray / Two strings / StatesP22GreedyWhen the best move now never hurts later7 problemsP23Divide and conquerWhen halves can be solved apart and joined easily6 problems · Merge sort / Build a tree