const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Min Cost Climbing Stairs: LeetCode 746 DP Walkthrough

Min Cost Climbing Stairs (#746) is Climbing Stairs with weights — the same Fibonacci recurrence but now you minimize cost instead of counting paths.

6 min read|

Climbing Stairs + costs = weighted Fibonacci DP

Same recurrence, different objective — minimize cost instead of counting paths

Min Cost Climbing Stairs — The Weighted Climbing Stairs

If you have solved Climbing Stairs (#70), you already know the core idea behind Min Cost Climbing Stairs (#746). Both problems use the same Fibonacci-style recurrence, but the objective flips from counting paths to minimizing cost. Instead of asking "how many ways can you reach the top?", this problem asks "what is the cheapest way to get there?"

That single change — adding a cost array — transforms a pure counting problem into an optimization problem. It is the natural next step in the 1-D dynamic programming progression, and it lays the groundwork for harder problems like House Robber (#198) that extend the same recurrence with adjacency constraints.

In this walkthrough, you will see the min cost climbing stairs leetcode recurrence, how to optimize it to O(1) space, and a visual trace through a concrete example so the logic clicks before you ever open your editor.

ℹ️

Pattern Connection

Min Cost Climbing Stairs (#746) is the bridge between Climbing Stairs (#70) and House Robber (#198) — it introduces costs to the Fibonacci recurrence, which House Robber extends with adjacency constraints.

Understanding the Problem

You are given an integer array cost where cost[i] is the price you pay to step on stair i. Once you pay that cost, you can climb either one or two steps. You can start from index 0 or index 1, and your goal is to reach past the last stair (index n) with the minimum total cost.

The key insight is that you are stepping past the array. The "top" is not cost[n-1] — it is the position after cost[n-1]. You can land on the top from either of the last two stairs, so the answer considers both ending positions.

This is what makes the leetcode 746 solution slightly tricky for first-timers. The top of the staircase is an implicit position beyond the array, and you must compare the cost of arriving there from step n-1 versus step n-2.

The DP Recurrence for Min Cost Climbing Stairs

Define dp[i] as the minimum cost to reach stair i (and pay its cost). The recurrence is straightforward: dp[i] = cost[i] + min(dp[i-1], dp[i-2]). At each stair, you pay cost[i] and arrive from whichever of the two previous stairs was cheaper.

The base cases are dp[0] = cost[0] and dp[1] = cost[1]. You can start from either step 0 or step 1, so there is no prior cost to add at these positions. You simply pay the cost of the starting stair.

The final answer is min(dp[n-1], dp[n-2]) — not just dp[n-1]. Since you can reach the top by taking one step from stair n-1 or two steps from stair n-2, you compare both and return the smaller value.

  • Recurrence: dp[i] = cost[i] + min(dp[i-1], dp[i-2])
  • Base cases: dp[0] = cost[0], dp[1] = cost[1]
  • Answer: min(dp[n-1], dp[n-2]) — you step PAST the last stair
  • Time complexity: O(n) — single pass through the array
  • Space complexity: O(n) with a dp array, O(1) with optimization
💡

Key Insight

The answer is min(dp[n-1], dp[n-2]), NOT dp[n-1] — you can reach the top by stepping from either of the last two stairs. Don't forget you're stepping PAST the array.

Space Optimization — O(1) with Two Variables

Just like Climbing Stairs, you only need the last two dp values at any point. Replace the entire dp array with two variables — prev2 and prev1 — and update them as you iterate. This drops space from O(n) to O(1) while keeping time at O(n).

Initialize prev2 = cost[0] and prev1 = cost[1]. For each stair from index 2 to n-1, compute current = cost[i] + min(prev1, prev2), then shift: prev2 = prev1, prev1 = current. After the loop, the answer is min(prev1, prev2).

This stairs optimization dp technique is identical to the Fibonacci number space optimization. Once you recognize the pattern — "I only need the last two values" — you can apply it to any linear recurrence that depends on two prior states.

Visual Walkthrough — cost = [10, 15, 20]

Let us trace through the example cost = [10, 15, 20] step by step. Start with dp[0] = 10 and dp[1] = 15 — these are the base cases since you can begin at either stair.

For stair 2: dp[2] = cost[2] + min(dp[1], dp[0]) = 20 + min(15, 10) = 20 + 10 = 30. You arrive at stair 2 from stair 0 because it is cheaper (cost 10 versus 15).

The answer is min(dp[1], dp[2]) = min(15, 30) = 15. The optimal path is to start at stair 1 (pay 15), then take two steps to the top, skipping stair 2 entirely. Total cost: 15.

  1. 1Base cases: dp[0] = 10, dp[1] = 15
  2. 2dp[2] = 20 + min(15, 10) = 30 — arrived from stair 0
  3. 3Answer: min(dp[1], dp[2]) = min(15, 30) = 15 — start at stair 1, skip stair 2
⚠️

Common Mistake

You can start from either step 0 or step 1 — this means dp[0]=cost[0], dp[1]=cost[1] (no choice at the starting steps). Don't add min(dp[-1], dp[-2]) to the base cases.

Edge Cases to Watch For

The minimum input size is two stairs. When cost has exactly two elements, the answer is simply min(cost[0], cost[1]) — pick the cheaper starting stair and step to the top. No loop is needed.

When all costs are the same, every path has identical cost. The answer is still min(dp[n-1], dp[n-2]), which depends on whether n is even or odd. When costs are strictly decreasing, the greedy approach of always stepping to the next stair might seem optimal, but the recurrence handles it automatically.

  • Two stairs only: answer = min(cost[0], cost[1])
  • All same cost: every path has the same total — the recurrence still works
  • cost[0] = 0: free start from stair 0, but the recurrence still chooses optimally
  • Decreasing costs: stepping forward is cheap, but skipping might still save overall

What Min Cost Climbing Stairs Teaches You

Min Cost Climbing Stairs is the simplest weighted Fibonacci DP problem. It bridges counting (Climbing Stairs #70) and constrained optimization (House Robber #198), making it the ideal second problem in the 1-D DP sequence.

The core lesson is that adding weights to a Fibonacci recurrence changes the objective but not the structure. You still depend on two prior states, you still optimize space to O(1), and you still build the answer bottom-up. Recognizing this pattern means you can adapt the same framework to dozens of DP problems.

Practice this pattern with YeetCode flashcards to lock in the recurrence. When you see "minimize cost along a linear sequence with step choices," your brain should immediately reach for dp[i] = cost[i] + min(dp[i-1], dp[i-2]).

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now