Filling a table from the bottom up
🌍 In real life. A shopkeeper keeps a ledger of the day's running total. Each line is the line before plus the next sale; nobody re-adds the whole day for each line. Rina's stairs work the same way: write down the ways to reach stair 0, then 1, then 2, each from the lines just above it, until you reach stair n.
The idea in one sentence: tabulation (bottom-up DP) fills a table of every state in an order where each cell's smaller states are already filled, so there is no recursion and no memo lookup, just a loop.
Its words:
- table: a list
dpwheredp[i]holds the answer to statei - bottom-up: filling from the base cases towards the whole question
- fill order: the order of the loop; every cell a cell reads must come earlier
- rolling variables: keeping only the last few cells when nothing older is read again
Step by step:
- make a list
dpwith one cell per state, 0 to n - fill the base cases:
dp[0] = 1, anddp[1] = 1 - for i from 2 to n:
dp[i] = dp[i − 1] + dp[i − 2] - the answer is
dp[n]
Watch the table for 6 stairs fill from left to right; the arcs show the two cells each new one reads:
Top-down or bottom-up? They do the same work. The memo (Lesson 57.1) works out only the states the question reaches and keeps the recurrence easy to read, but it recurses n deep. The table needs you to find a fill order, then runs as a plain loop with no recursion limit. Both are O(n) here.
Saving memory. Each cell reads only the two before it, so once a cell is filled, everything older is never read again. Two variables are enough: O(1) memory instead of O(n).
What it costs, by counting. n − 1 additions for n stairs: 999 additions for 1,000 stairs, where the plain recursion would make more calls than there are atoms on Earth.
def climb_table(n):
dp = [0] * (n + 1) # dp[i]: ways to reach stair i
dp[0] = 1 # standing at the bottom is one way
for i in range(1, n + 1):
dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)
return dp[n]
def climb_two(n):
before, last = 1, 1 # dp[i - 2] and dp[i - 1]
for _ in range(2, n + 1):
before, last = last, before + last
return last
print(climb_table(6), climb_two(6))
print(climb_table(1000) == climb_two(1000))
print(len(str(climb_two(1000))), "digits")Mistakes and edge cases.
- the wrong base case:
dp[0] = 0gives the Fibonacci numbers, one place behind; standing at the bottom is one way - an index error for small n: with n = 0,
dp[1]doesn't exist; guard it, or start the loop so it never reads outside the list - the wrong fill order: filling from n down to 0 reads cells that are still 0
- swapping the rolling variables one at a time:
before = lastthenlast = before + lastuses the newbefore; update both at once with a tuple
Quiz
In what order must a table be filled?
- So every cell a cell reads is filled before it
- From the answer cell down to the base cases
- Any order: the values sort themselves out
What does this print?
def climb_table(n): dp = [0] * (n + 1) # dp[i]: ways to reach stair i dp[0] = 1 # standing at the bottom is one way for i in range(1, n + 1): dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0) return dp[n] print(climb_table(7))What does this print?
before, last = 1, 1 for _ in range(2, 5): before, last = last, before + last print(before, last)Why are two variables enough for the stairs?
- Each cell reads only the two before it
- The answer is always small
- Python lists can't be long
climb_table(4) gives 0 instead of 5. Which line is wrong?
def climb_table(n): dp = [0] * (n + 1) dp[0] = 1 for i in range(n, 0, -1): dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0) return dp[n]
Coding tasks
Write
climb_table(n): the number of ways to climbnstairs taking 1 or 2 at a time, by filling a table from the bottom up (no recursion). There is 1 way to climb 0 stairs.Write
tribonacci(n): T(0) = 0, T(1) = 1, T(2) = 1, and after that each number is the sum of the three before it. Fill it bottom up.