Dynamic Programming Cheat Sheet

Cache each recursive call's result. Overlapping subproblems get solved once instead of over and over.

Dynamic Programming cheat sheet preview

Earn the full sheet below.

When to use it

  • The problem breaks into smaller, overlapping subproblems.
  • Brute force recursion redoes the same work.
  • It counts ways or finds a min or max over a series of choices.

What's on the sheet

Steps 1, 2, and 4 barely change. Step 3 is the whole problem, and 2D problems grow the memo to match.

  1. Base Case Check. The recursion stops at the simplest known values.
  2. Memoization Check. Solved before? Return the cached answer.
  3. Recurrence Relation. The recurrence IS the problem. One answer builds from smaller ones.
  4. Memoization List Initialization. memo starts as n+1 slots of None.

Watch out

  • Cache the result before you return it, not just when you compute it. Skip that and every call recomputes its whole subtree, right back to O(2^n).
  • memo needs n+1 slots, not n. This off-by-one throws an index error the moment you ask for the top value.
  • For a 2D memo, use [[None] * cols for i in range(rows)]. [[None] * cols] * rows duplicates one inner list across every row.

How to earn it

Solve half of the Dynamic Programming problems and the sheet is yours. A free account works fine, and everyone earns their sheets the same way.

Want to see a finished sheet first? Preview the Two Pointers sample.

Every pattern has a sheet. See the full set.