Min Cost Climbing Stairs (LeetCode 746): Solution and Explanation
Each step of a staircase has a cost you pay when you leave it, and from any step you can climb one or two steps. You may start on step 0 or step 1. Find the cheapest way to get past the last step to the top.
Watch it run step by step
Try your own input and write the code →Intuition
A recursive brute force tries both moves from every step: pay the cost and climb one, or pay and climb two. The number of paths roughly doubles with each step, so this explodes exponentially, even though most of the work is repeated. The cheapest way to reach step 5 is recomputed every time any path passes through it.
Flip the question around and ask how you could arrive at a given position. To stand on step i, your last move came either from step i - 1 (paying cost[i-1] to leave it) or from step i - 2 (paying cost[i-2]). So the cheapest way to reach i is the smaller of those two options, each built on the cheapest way to reach the earlier step. That gives the recurrence dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]).
The base cases come straight from the rules. You can start on step 0 or step 1 for free, so dp[0] = 0 and dp[1] = 0. The top of the stairs is position n, one past the last step, and the answer is dp[n].
Filling this table from left to right means every value is computed once using values that are already final, which turns the exponential recursion into a single linear pass. Since each entry only looks two positions back, the table can also be squeezed into two variables if memory matters.
Approach: Bottom-up dynamic programming
- Let
n = len(cost)and createdpof lengthn + 1filled with 0. Entries 0 and 1 stay 0, since starting there is free. - For each
ifrom 2 ton, consider arriving from one step below:dp[i-1] + cost[i-1]. - Also consider arriving from two steps below:
dp[i-2] + cost[i-2]. - Store the smaller of the two in
dp[i]. - Return
dp[n], the cheapest cost to reach the top, which lies just past the last step.
Step-by-step example
cost = [10,15,20]
nis 3, sodphas four entries, anddp[0] = dp[1] = 0.- For
i = 2: arriving from step 1 costs 0 + 15 = 15, and arriving from step 0 costs 0 + 10 = 10. Sodp[2] = 10. - For
i = 3: arriving from step 2 costs 10 + 20 = 30, and arriving from step 1 costs 0 + 15 = 15. Sodp[3] = 15. - The top is position 3, so the answer is 15.
- That corresponds to starting on step 1, paying 15, and climbing two steps straight to the top.
Code: Python, Java, C++
1class Solution:
2 def minCostClimbingStairs(self, cost: List[int]) -> int:
3 n = len(cost)
4 dp = [0] * (n + 1)
5
6 for i in range(2, n + 1):
7 dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2])
8
9 return dp[n]1class Solution {
2 public int minCostClimbingStairs(int[] cost) {
3 int n = cost.length;
4 int[] dp = new int[n + 1];
5
6 for (int i = 2; i <= n; i++) {
7 dp[i] = Math.min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]);
8 }
9 return dp[n];
10 }
11}1class Solution {
2public:
3 int minCostClimbingStairs(vector<int>& cost) {
4 int n = cost.size();
5 vector<int> dp(n + 1, 0);
6
7 for (int i = 2; i <= n; i++) {
8 dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]);
9 }
10 return dp[n];
11 }
12};Time and space complexity
O(n)
The loop runs once for each position from 2 to n and does a constant amount of work for each.
O(n)
The dp array has n + 1 entries. Because each entry only depends on the two before it, this can be reduced to O(1) with two rolling variables.
Other approaches
Two rolling variables (time O(n), space O(1))
Keep only the last two dp values and slide them forward on each step. The recurrence is identical, but memory drops to a constant, which is the usual follow-up.
Top-down recursion with memoization (time O(n), space O(n))
Define the minimum cost from step i to the top recursively and cache each result. It mirrors the problem description closely and runs in linear time, but uses a call stack and a memo table.
Common mistakes
- Returning
dp[n - 1]instead ofdp[n]. The top is one position past the last step, not the last step itself. - Setting
dp[0] = cost[0]anddp[1] = cost[1]while also adding costs in the recurrence, which charges for the starting step twice. Decide whether dp means the cost to arrive at a step or the cost to leave it, and stick to one definition. - Forgetting that you can start on step 1, which forces every path through step 0 and overcounts.
- Writing the plain recursion without memoization, which is correct on small inputs but exponential on the full constraints.
Frequently asked questions
What does dp[i] mean in Min Cost Climbing Stairs?
Here dp[i] is the minimum total cost to arrive at position i. You pay a step's cost when you leave it, so arriving at step 0 or 1 is free, and dp[n] is the cost to reach the top.
Can Min Cost Climbing Stairs be solved in O(1) space?
Yes. Each value depends only on the previous two, so two variables are enough. Update them in a loop and return the last one.
How is this different from Climbing Stairs?
Climbing Stairs counts the number of ways to reach the top, which adds the two previous values. This problem minimizes a cost, so it takes the minimum of the two previous options plus their step costs.