Browse curriculum

Solving a Question with DP

A systematic 5-step framework for solving any Dynamic Programming problem.

Medium
8 min

Examples

Input:cost = [10,15,20]
Output:15
Explain:

Start on step 1, pay 15, and stride straight past the top. Step 2 is never used.

Input:cost = [1,100,1,1,1,100,1,1,100,1]
Output:6
Explain:

Start on step 0 and take a two-step stride over each 100. Six cheap steps are paid for.

Input:cost = [5,7]
Output:5
Explain:

The loop body never runs past its base cases: the cheaper of the two starting steps is the whole answer.

The Framework

Solving DP problems can be daunting. Use this 5-step framework to break it down systematically.

Step 1: Define the Objective

What are we minimizing, maximizing, or counting?

  • Example: "Find the minimum cost to climb stairs."

Step 2: Identify the State

What variables uniquely define our progress? Minimize the number of state variables.

  • Example: i = current stair index.
  • State: dp[i]

Step 3: Recurrence Relation (Transitions)

How do we move from one state to another? This is the core equation.

  • Example: dp[i] = cost[i] + min(dp[i-1], dp[i-2])

Step 4: Base Cases

When does the recursion stop? What are the trivial answers?

  • Example: dp[0] = cost[0], dp[1] = cost[1]

Step 5: Implementation (Memo vs Tabulation)

Decide whether to write it top-down (recursion plus a memo) or bottom-up (a loop plus a table).

  • Top-down: closer to the recurrence you just wrote, and it only ever visits states it needs.
  • Bottom-up: no call stack to overflow, and it opens the door to keeping only the last row or two.

The walkthrough below is the bottom-up version of the five steps above.

Interactive Walkthrough (Min Cost Climbing Stairs)

ROW cost = WHAT A STEP CHARGES · ROW dp = CHEAPEST TOTAL TO STAND THERE · OUTLINED = THE TWO CELLS IT IS BUILT FROM

NEXT STEP OF THE STAIRCASE

0

1

2

cost

10
15
20

dp

-
-
-

steps

3

filled

0/3

Answer

—

the five steps

1. OBJECTIVE — LEAST TOTAL COST TO GET PAST THE TOP
2. STATE — dp[i], CHEAPEST TOTAL TO STAND ON STEP i
3. TRANSITION — dp[i] = cost[i] + min(dp[i−1], dp[i−2])
4. BASE — dp[0] = cost[0], dp[1] = cost[1]
5. ANSWER — min(dp[n−1], dp[n−2])

 

Take step 0, which charges 10

Next

You may start on step 0 or step 1, so step 0 costs only itself: dp[0] = 10

cost

Legend & Complexity

The cell being written

The two cells it is built from

A dash: not computed yet

Time

O(n)

Space

O(n)

Space, rolled

O(1)

Row cost is what each step charges; row dp is the cheapest total to be standing on it. Watch the two outlined cells feed the one being written — that is step 3, the transition, doing its work.

Try Skip last: dp[2] is not the answer, and the last frame shows why.

Example: Climbing Stairs

The five steps, written out. The base cases sit inside the loop so every step of the staircase is handled the same way.

Solving a Question with DP Code
1var minCostClimbingStairs = function(cost) {
2 const n = cost.length;
3 const dp = new Array(n).fill(0);
4
5 for (let i = 0; i < n; i++) {
6 if (i < 2) {
7 dp[i] = cost[i];
8 continue;
9 }
10 const best = Math.min(dp[i - 1], dp[i - 2]);
11 dp[i] = cost[i] + best;
12 }
13
14 return Math.min(dp[n - 1], dp[n - 2]);
15};

The approach, step by step

  1. Name the objective and the state

    Say what you are optimising, then choose the fewest variables that describe a subproblem — here, the step index.

  2. Write the recurrence and the base cases

    Express dp[i] in terms of earlier states, and decide what the smallest states are worth before the loop can use them.

  3. Fill the table and read the answer off it

    Iterate the states in an order that has their inputs already computed, then read the answer from the cells that can reach the goal.

Frequently asked questions

What are the five steps for solving a dynamic programming problem?

Name the objective (what is being minimised, maximised or counted); identify the state that uniquely describes a subproblem; write the recurrence that moves between states; pin down the base cases; then implement it top-down with memoization or bottom-up with a table.


How do I find the state of a DP problem?

Ask what you would need to know to finish the job from here, and use as few variables as will answer that. In Min Cost Climbing Stairs the index alone is enough, so the state is dp[i] — one number per step.


Why is the answer min(dp[n-1], dp[n-2]) and not dp[n-1]?

dp[i] is the cost of standing on step i, and the goal is the floor past the top. From the second-to-last step a single stride clears the top without paying for the last step at all, so both endings have to be compared.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description