Watch 10 video solutions for Min Cost Climbing Stairs, a easy level problem involving Array, Dynamic Programming. This walkthrough by NeetCode has 178,140 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |