Module 10 · Dynamic Programming and Greedy · Lesson 34 of 40

Dynamic programming: remember, don't repeat

When a recursion solves the same small problems again and again, save each answer once. Memoization and tabulation.

In this lesson · about 24 minutes

  • Spot overlapping subproblems
  • Speed up recursion with memoization (a cache)
  • Fill a table bottom-up (tabulation) and shrink it to two variables

Remember the recursive Fibonacci from Module 6? fib(5) calls fib(4) and fib(3). But fib(4) also calls fib(3). The same small problems are solved again and again, and the number of calls explodes (about 2ⁿ).

Dynamic programming (DP) fixes this with one simple idea: solve each small problem once, write the answer down, and reuse it.

When does DP help?

  1. Overlapping subproblems: the same smaller question comes up many times.
  2. Optimal substructure: the best answer to the big problem is built from best answers to smaller ones.

Way 1: memoization (top-down)

Keep the recursion, but add a dict (the "memo"). Before working, check if the answer is already saved.

memo = {}
def fib(n):
    if n < 2:
        return n
    if n in memo:
        return memo[n]
    memo[n] = fib(n - 1) + fib(n - 2)
    return memo[n]

Python can do this for you: put @functools.cache above the function.

Way 2: tabulation (bottom-up)

Skip recursion. Make a table and fill it from the smallest case upward.

dp = [0] * (n + 1)
dp[1] = 1
for i in range(2, n + 1):
    dp[i] = dp[i - 1] + dp[i - 2]

If each answer only needs the last two, keep just two variables: O(1) space.

Try it · see it work

Dynamic programming: climbing stairs

Ways to climb n stairs taking 1 or 2 steps at a time. Watch the table fill, and compare with the number of plain recursive calls.

Number of steps n =
10
11
?2
?3
?4
?5
?6
?7
?8

Step 1 of 9Base cases: there is 1 way to stand on step 0 and 1 way to reach step 1.

Plain recursion without remembering answers would make 67 calls for n = 8. The table fills 9 boxes. That is the power of dynamic programming: solve each small problem once and reuse it.

How to design a DP solution

  1. State: what does dp[i] mean, in words? ("the number of ways to reach step i")
  2. Transition: how is dp[i] built from smaller states? (dp[i - 1] + dp[i - 2])
  3. Base cases: the smallest answers you know directly. (dp[0] = 1, dp[1] = 1)
  4. Order: fill smaller states before the ones that need them.
  5. Answer: which state holds the final answer? (dp[n])

Cost of DP ≈ (number of states) × (work per state). Climbing stairs has n states and O(1) work each: O(n).

Try it: count the calls

Exercise

Climbing stairs (table)

You climb a staircase of n steps, taking 1 or 2 steps at a time. Write climb(n) that returns the number of different ways, using a table: ways[0] = 1, ways[1] = 1, and ways[i] = ways[i - 1] + ways[i - 2].

Exercise

Paths in a grid (memo)

A robot starts at the top-left of a grid with rows × cols cells and may only move right or down. Write paths(r, c) that returns the number of routes to the bottom-right corner. Use a memo dict. Base case: a single row or a single column has exactly 1 path.

Check your understanding0 of 3 correct

Question 1

1.What problem does dynamic programming fix?

Question 2

2.Memoization is…

Question 3

3.A DP has n × m states and does O(1) work per state. Time?

Quiz score: 0 of 3

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.