Bottom-Up Dynamic Programming
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Why Bottom-Up
From asking to building
Now that we've covered Recursive Approach, Memoization, and Top-Down Dynamic Programming, you've seen the same pattern evolve:
- Start with a recursive contract.
- Notice repeated subproblems.
- Cache answers so each state is solved once.
Bottom-up dynamic programming flips the direction. Instead of asking, "What do I need to solve this?" and recursively diving downward, we build the smallest answers first and work upward.
Think of it like infrastructure provisioning: if a service depends on a database and a secret, you don't deploy the service first and hope the dependencies appear. You build dependencies in a safe order, then consume them.
That's the core of bottom-up DP: fill states in dependency order.

The Table Mindset
Bottom-up means tabulation
Bottom-up DP is often called tabulation because we usually store answers in a table or array.
The table represents states you've already solved:
state: 0 1 2 3 4 5
value: [?, ?, ?, ?, ?, ?]
The job is not to recurse. The job is to answer two questions:
- What are the base states?
- In what order can every later state use only earlier known states?
If top-down DP feels like lazy computation on demand, bottom-up feels like an explicit build pipeline. You decide the order up front.

Fibonacci Tabulated
Same recurrence, different direction
In earlier lessons, Fibonacci was useful because its dependency shape is simple:
fib(n) = fib(n - 1) + fib(n - 2)
Top-down asks for fib(n) first and recursively discovers fib(n - 1) and fib(n - 2).
Bottom-up starts with fib(0) and fib(1), then fills forward until it reaches n.
```javascript
function fibBottomUp(n) {
if (n < 0) throw new Error("n must be non-negative");
const dp = Array(n + 1).fill(0);
dp[0] = 0;
if (n >= 1) dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
console.log(fibBottomUp(0)); // 0
console.log(fibBottomUp(1)); // 1
console.log(fibBottomUp(8)); // 21
```
Order Check
In bottom-up DP, what makes a loop order valid?
Grid Paths Table
A 2D table example
In Memoization, you solved grid paths by caching (row, col) states. Bottom-up uses the same state idea, but fills the grid directly.
For a grid with rows and cols, let:
dp[r][c] = number of ways to reach cell (r, c)
If you can only move down or right, then each cell can be reached from:
- the cell above:
dp[r - 1][c] - the cell to the left:
dp[r][c - 1]
So the transition is:
dp[r][c] = dp[r - 1][c] + dp[r][c - 1]
```javascript
function countGridPaths(rows, cols) {
const dp = Array.from({ length: rows }, () => Array(cols).fill(0));
// Base case: there is one way to stand on the starting cell.
dp[0][0] = 1;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (r === 0 && c === 0) continue;
const fromAbove = r > 0 ? dp[r - 1][c] : 0;
const fromLeft = c > 0 ? dp[r][c - 1] : 0;
dp[r][c] = fromAbove + fromLeft;
}
}
console.log(dp.map(row => row.join(" ")).join("\n"));
return dp[rows - 1][cols - 1];
}
console.log("Paths:", countGridPaths(3, 4));
// Table:
// 1 1 1 1
// 1 2 3 4
// 1 3 6 10
// Paths: 10
```
Memory Assumption
True or false: Bottom-up DP always uses less memory than top-down DP with memoization.
Step Cost Bottom-Up
Choices become transitions
In Top-Down Dynamic Programming, we used step-cost style problems to model branching choices. Bottom-up keeps the same meaning, but computes costs from the start forward.
Suppose cost[i] is the cost of stepping on stair i. You may climb either 1 or 2 stairs at a time.
Let:
dp[i] = minimum cost to reach stair i
To reach stair i, you came from either i - 1 or i - 2:
dp[i] = cost[i] + min(dp[i - 1], dp[i - 2])
The key is that by the time we compute dp[i], both previous answers already exist.
```javascript
function minCostToReachLastStair(cost) {
const n = cost.length;
if (n === 0) return 0;
if (n === 1) return cost[0];
const dp = Array(n).fill(0);
dp[0] = cost[0];
dp[1] = cost[1];
for (let i = 2; i < n; i++) {
dp[i] = cost[i] + Math.min(dp[i - 1], dp[i - 2]);
}
console.log("dp table:", dp);
return dp[n - 1];
}
console.log(minCostToReachLastStair([10, 15, 20])); // 30
console.log(minCostToReachLastStair([1, 100, 1, 1, 1])); // 3
```
Name The Seeds
In bottom-up DP, the smallest already-known states placed into the table before the main recurrence loop are called ____.
Dependency Debugging
When bottom-up goes wrong
Most bottom-up bugs are dependency bugs. When a value looks wrong, inspect the table like you would inspect a failed deployment graph or CI pipeline.
Ask:
- Did I initialize the base cases correctly?
- Does each state mean exactly one thing?
- Does the loop order guarantee dependencies are already filled?
- Are boundaries handled intentionally?
- Am I returning the state that matches the question?
A useful habit: before coding, write one sentence for the state.
dp[i] means ...
dp[r][c] means ...
If that sentence is fuzzy, the implementation usually gets fuzzy too.

Bottom-Up Recipe
The practical recipe
Bottom-up DP is not a different problem-solving universe from top-down DP. It's the same dependency graph, evaluated in a different direction.
Use this workflow:
Define the state
What does each table entry mean?Identify base cases
Which answers are known without more work?Write the transition
How does a bigger state use smaller states?Choose loop order
Make sure dependencies are computed first.Return the right state
Match the original question exactly.
Bottom-up DP is especially powerful when the state space is regular: arrays, grids, prefixes, lengths, capacities, and time steps. Next, we'll look at ways to keep the same logic while reducing unnecessary work or storage.
