Skip to main content

Updated Sep 11, 2026

Problem-Solving Patterns Cheat Sheet

This page is a reference, not a tutorial — see Problem-Solving Patterns Overview for a first read through the folder. Every complexity below names its case (best / average / worst); the full argument for each lives on that pattern's own page, alongside its worked trace.

"What does the input and the question look like?" → reach for…​

Signal in the input or questionPatternPage
Sorted array, or "find a pair/triplet meeting a condition"Two PointersTwo Pointers & Sliding Window
"Longest/shortest contiguous subarray or substring satisfying…"Sliding WindowTwo Pointers & Sliding Window
A problem splits cleanly into same-shaped, independent halvesDivide & ConquerDivide & Conquer
Local best-choice-now is claimed (or provable) to reach the global optimumGreedyGreedy Algorithms
"Count the ways to…" / "minimum/maximum way to…", with overlapping subproblemsDynamic ProgrammingDynamic Programming
One DP recurrence needs to go from a first draft to a fast, space-tight implementationRecursion → Memoization → TabulationRecursion, Memoization & Tabulation
"Generate all…" / "find every arrangement/subset satisfying a constraint"BacktrackingBacktracking
Many range-sum queries over data that does not change, or many range updates read once at the endPrefix Sums / Difference ArraysPrefix Sums & Difference Arrays
"Next greater/smaller element", or "largest rectangle/window extreme"Monotonic Stack / QueueMonotonic Stack & Queue
A list of (start, end) ranges: overlap count, merging, or free-room schedulingIntervals & Sweep LineIntervals & Sweep Line
A linked list or state -> next_state function, cycle detection or midpoint with O(1)O(1) spaceFast & Slow PointersFast & Slow Pointers
"Top/bottom k of…", or the input is a stream too large or too long to hold in fullTop-K & StreamingTop-K & Streaming

Complexity per pattern​

PatternTypical timeExtra spaceCase named
Two PointersO(n)O(n)O(1)O(1)worst
Sliding WindowO(n)O(n)O(1)O(1) or O(k)O(k) with a window structureamortized
Divide & ConquerO(nlog⁡n)O(n \log n) typical (Master Theorem case-dependent)O(log⁡n)O(\log n) call stackworst
GreedyO(nlog⁡n)O(n \log n), dominated by a sortO(1)O(1) beyond the sortworst
Dynamic ProgrammingO(statesxtransitions)O(states x transitions)O(states)O(states), or O(1row)O(1 row) rollingworst
Recursion → Memoization → TabulationO(2n)O(2^{n}) naive -> O(states)O(states) memoized/tabulatedO(states)O(states) -> O(1row)O(1 row) rollingworst, see the page's four-form table
BacktrackingExponential, pruned in practiceO(depth)O(depth) call stackworst
Prefix Sums / Difference ArraysO(n)O(n) build, O(1)O(1) queryO(n)O(n)worst
Monotonic Stack / QueueO(n)O(n) amortized (each element pushed/popped once)O(n)O(n)amortized
Intervals & Sweep LineO(nlog⁡n)O(n \log n), dominated by sorting endpointsO(n)O(n)worst
Fast & Slow PointersO(n)O(n)O(1)O(1)worst
Top-K & StreamingO(nlog⁡k)O(n \log k) heap, O(n)O(n) reservoir samplingO(k)O(k)worst

Template-code index​

Each pattern's page carries a ## Mechanism section with the worked trace and the <Tabs> skeleton code. Where a page splits the mechanism into named subsections, the skeleton to copy lives in the one named below rather than at the top of ## Mechanism itself.

PatternSection carrying the skeleton
Two Pointers & Sliding Window### Two pointers on sorted data and ### Sliding window
Divide & Conquer## Mechanism
Greedy Algorithms### Where greedy works: interval scheduling
Dynamic Programming### The workflow, on a real problem
Recursion, Memoization & Tabulation## Mechanism — all four forms, in one place
Backtracking### N-queens
Prefix Sums & Difference Arrays## Mechanism
Monotonic Stack & Queue## Mechanism
Intervals & Sweep Line### Counting overlaps by sorting endpoints, not intervals
Fast & Slow Pointers## Mechanism
Top-K & Streaming## Mechanism

Decision flow​

The two questions worth double-checking before committing to an answer: "do the subproblems actually overlap" (the line between Divide & Conquer and Dynamic Programming), and "is the greedy choice provably optimal, or just plausible" (the line between Greedy and Backtracking) — see Greedy Algorithms for a worked case where a plausible-looking greedy choice is provably wrong.

Recall​

References​

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed., Ch. 4 (divide and conquer), Ch. 14 (dynamic programming), Ch. 15 (greedy) — the chapters this page's table summarises.