You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Example 1:
Input: cost = [10,15,20] Output: 15 Explanation: You will start at index 1. - Pay 15 and climb two steps to reach the top. The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1] Output: 6 Explanation: You will start at index 0. - Pay 1 and climb two steps to reach index 2. - Pay 1 and climb two steps to reach index 4. - Pay 1 and climb two steps to reach index 6. - Pay 1 and climb one step to reach index 7. - Pay 1 and climb two steps to reach index 9. - Pay 1 and climb one step to reach the top. The total cost is 6.
Constraints:
2 <= cost.length <= 10000 <= cost[i] <= 999Problem Overview: You are given an array cost where cost[i] represents the cost of stepping on the i-th stair. You can climb either one or two steps at a time, and you may start from step 0 or step 1. The goal is to reach the top of the staircase (one step beyond the last index) with the minimum total cost.
Approach 1: Dynamic Programming with Table (O(n) time, O(n) space)
This approach models the problem using dynamic programming. Define dp[i] as the minimum cost required to reach step i. Since you can arrive at step i from either i-1 or i-2, the recurrence becomes dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]). Start with dp[0] = 0 and dp[1] = 0 because you can begin from either step without paying a cost. Iterate through the array, computing the optimal cost for each position until you reach the top. This approach clearly shows the subproblem structure and is easy to reason about during interviews. Time complexity is O(n) because each step is processed once, and space complexity is O(n) for the DP table.
Approach 2: Optimized Dynamic Programming with Constant Space (O(n) time, O(1) space)
The transition only depends on the previous two states, so storing the entire DP table is unnecessary. Track just two variables representing the minimum cost to reach the previous two steps. For each new step, compute the current minimum using the same recurrence: current = min(prev1 + cost[i-1], prev2 + cost[i-2]). Then shift the variables forward for the next iteration. This reduces memory usage while preserving the same logic as the table-based solution. The algorithm still iterates once through the input array, giving O(n) time complexity and O(1) extra space.
The key insight is recognizing overlapping subproblems. The minimum cost to reach a step depends only on the two steps before it, which makes the problem a classic dynamic programming pattern similar to Fibonacci-style state transitions.
Recommended for interviews: Start by explaining the DP state definition and recurrence using the table approach. It demonstrates that you understand how to break the problem into subproblems. Then optimize the solution by keeping only the last two states to achieve constant space. Interviewers typically expect the O(n) time and O(1) space dynamic programming solution.
This approach uses a dynamic programming table to store the minimum cost at each step. The key idea is that to reach step i, you can come from step i-1 or step i-2, and you need to pay the cost associated with landing on step i. Thus, the cost to reach step i is the minimum of the cost to reach steps i-1 or i-2 plus cost[i]. The solution returns the minimum cost to reach the top from the last two steps.
This solution initializes a dynamic programming array to handle the cost calculations. It then iterates through the cost array, updating the dp array with the minimum cost to reach each step by considering the costs of the previous two steps. The result is the minimum of the values from the last two steps, representing the total minimum cost to reach the top.
Time Complexity: O(n), where n is the number of steps as we traverse the list once.
Space Complexity: O(n), as we need an additional array to store minimum costs at each step.
This approach optimizes the space usage of the dynamic programming solution by only retaining two variables to store the minimal costs for the last two steps, instead of an entire table. This is possible because each step's cost only depends on the previous two calculations. You update these two variables iteratively to find the minimum cost to reach the top.
This C solution reduces space by using two variables, prev and curr, to store the two previous minimum costs. As it iterates through the cost array, it updates these variables based on their minimum and the current cost. Finally, it returns the minimum of the last two values stored in prev and curr.
Time Complexity: O(n)
Space Complexity: O(1)
We design a function dfs(i), which represents the minimum cost required to climb the stairs starting from the i-th step. Therefore, the answer is min(dfs(0), dfs(1)).
The execution process of the function dfs(i) is as follows:
i \ge len(cost), it means the current position has exceeded the top of the stairs, and there is no need to climb further, so return 0;1 step with a cost of cost[i], then recursively call dfs(i + 1); or we can choose to climb 2 steps with a cost of cost[i], then recursively call dfs(i + 2);To avoid repeated calculations, we use memoization search, saving the results that have already been calculated in an array or hash table.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the array cost.
Go
Java
Rust
C++
JavaScript
TypeScript
Python
We define f[i] as the minimum cost needed to reach the i-th stair. Initially, f[0] = f[1] = 0, and the answer is f[n].
When i \ge 2, we can reach the i-th stair directly from the (i - 1)-th stair with one step, or from the (i - 2)-th stair with two steps. Therefore, we have the state transition equation:
$
f[i] = min(f[i - 1] + cost[i - 1], f[i - 2] + cost[i - 2])
The final answer is f[n].
The time complexity is O(n), and the space complexity is O(n), where n is the length of the array cost$.
Python
Java
C++
Go
TypeScript
Rust
JavaScript
We notice that the state transition equation for f[i] only depends on f[i - 1] and f[i - 2]. Therefore, we can use two variables f and g to alternately record the values of f[i - 2] and f[i - 1], thus optimizing the space complexity to O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
| Approach | Complexity |
|---|---|
| Dynamic Programming with Table | Time Complexity: O(n), where n is the number of steps as we traverse the list once. |
| Optimized Dynamic Programming with Constant Space | Time Complexity: O(n) |
| Memoization Search | — |
| Dynamic Programming | — |
| Dynamic Programming (Space Optimization) | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Table | O(n) | O(n) | When learning or explaining DP state transitions clearly in interviews |
| Optimized Dynamic Programming (Constant Space) | O(n) | O(1) | Preferred production and interview solution when memory optimization matters |
Min Cost Climbing Stairs - Dynamic Programming - Leetcode 746 - Python • NeetCode • 178,140 views views
Watch 9 more video solutions →Practice Min Cost Climbing Stairs with our built-in code editor and test cases.
Practice on FleetCode