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] <= 999This 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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(1)
| 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) |
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python • NeetCode • 721,906 views views
Watch 9 more video solutions →Practice Min Cost Climbing Stairs with our built-in code editor and test cases.
Practice on FleetCode