AlgoFrames

Tabulation: filling the table bottom-up

Tabulation fills a table from the smallest subproblems up, so every value it needs is already there. Watch the table fill left to right.

Before you start: Overlapping subproblems and memoization.

The idea

Seven empty cells, one per step of a staircase. Fill them left to right and the last cell holds the answer.

Each cell stores the number of ways to reach one step. A table like this is the whole idea of tabulation.

Intuition

Cell 4 is built from cells 3 and 2: 3 + 2 = 5. A cell only reads cells with smaller indices.

The last move onto step 4 came from step 3 or step 2. So ways(4) = ways(3) + ways(2).

Fill from index 0 upward. When cell 5 is next, cells 0 to 4 are already done, so every input is ready.

Memoization reaches the same values by recursing from the top. Tabulation starts at the bottom, so it needs no recursion and no call stack.

Complexity

The loop fills n − 1 cells with one addition each: O(n) time. The full table keeps n + 1 cells, so space is O(n).

Each cell reads two earlier cells, but only the last two are ever needed. Two variables can replace the table and cut the space to O(1).

Keep a window of the last values instead of the table. Each pass computes the next value and slides the window along.

This works when a cell reads only a fixed number of cells just before it. The climb reads two, so the window holds two.

Pitfalls

Fill cell 4 before cell 3 and it reads an empty cell. The empty cell counts as 0, so cell 4 holds 2 instead of 5.

The loop order must match the reads. If a cell reads smaller indices, loop upward. A cell that reads larger indices needs a loop that runs downward.

To reach step n the table needs n + 1 cells, because cell 0 counts. A table of n cells makes dp[n] an index error.

Off-by-one table sizes are the most common tabulation bug. Write the largest index you will read, then add 1.

Variations

A cell can read three cells instead of two. The loop and the order stay the same; only the transition changes.

Every table fill answers the same five questions: state, transition, base cases, order and answer. The next topic names them.

Code

Pseudocode

climb(n):
  if n ≤ 1: return 1
  dp ← array of size n + 1
  dp[0] ← 1
  dp[1] ← 1
  for i from 2 to n:
    dp[i] ← dp[i - 1] + dp[i - 2]
  return dp[n]

Python

def climb(n):
    if n <= 1:
        return 1
    dp = [0] * (n + 1)
    dp[0] = 1
    dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    return dp[n]

JavaScript

function climb(n) {
  if (n <= 1) return 1;
  const dp = new Array(n + 1).fill(0);
  dp[0] = 1;
  dp[1] = 1;
  for (let i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] + dp[i - 2];
  }
  return dp[n];
}

Java

static int climb(int n) {
    if (n <= 1) return 1;
    int[] dp = new int[n + 1];
    dp[0] = 1;
    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    return dp[n];
}

Practice problems (7)

Fibonacci with a table Warm-up

Return fib(n), where fib(0) = 0, fib(1) = 1 and fib(i) = fib(i - 1) + fib(i - 2).

Fill a table from index 0 upward. Do not use recursion.

Fill the table by hand Warm-up

The table holds the number of ways to climb to each step, moving 1 or 2 steps at a time. Work out the next cell before the trace writes it. Each cell is the sum of the two cells before it.

Order of filling Warm-up

Cell i of the climbing table reads cells i − 1 and i − 2, and cells 0 and 1 are base cases. Drag the five cells into the order a correct tabulation fills them.

Climbing stairs Standard

A staircase has n steps. Each move climbs 1 step or 2 steps. Return the number of different move sequences that reach the top.

Fill a table where cell i holds the number of ways to reach step i.

Ways to make a total from 1, 3 and 4 Standard

Count the ordered ways to write n as a sum of the numbers 1, 3 and 4. The order counts: 1 + 3 and 3 + 1 are different ways.

For n = 0 the empty sum is the one way.

Spot the bug: table size Standard

This function should return the number of ways to climb n steps with moves of 1 or 2, but it crashes or returns the wrong value. Click the line with the bug, then fix it.

Tribonacci with O(1) space Challenge

The tribonacci numbers are T(0) = 0, T(1) = 1, T(2) = 1 and T(i) = T(i - 1) + T(i - 2) + T(i - 3). Return T(n).

Use O(1) extra space: keep only the last three values, not a table of n + 1 cells.

Related topics

Next on the path: The DP recipe: state, transition, base cases, order, answer