- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Min Cost Climbing Stairs in Python
Suppose there is a staircase, here the i-th step will be some non-negative cost value cost[i] assigned. When we pay the cost, we can either climb one or two steps. We have to find minimum cost to reach the top of the floor, and we also can either start from the step with index 0, or the step with index 1.
So, if the input is like cost = [12,17,20], then the output will be 17, The cheapest position to start from step 1 as we have to pay that cost and go to the top.
To solve this, we will follow these steps −
- dp := an array of size same as cost, and fill with 0
- dp := cost
- if size of cost >= 2, then
- dp := cost
- for i in range 2 to size of cost - 1, do
- dp[i] := cost[i] + minimum of dp[i-1], dp[i-2]
- return minimum of dp[-1], dp[-2]
Let us see the following implementation to get better understanding −
class Solution: def minCostClimbingStairs(self, cost): dp =  * len(cost) dp = cost if len(cost) >= 2: dp = cost for i in range(2, len(cost)): dp[i] = cost[i] + min(dp[i-1], dp[i-2]) return min(dp[-1], dp[-2]) ob = Solution() print(ob.minCostClimbingStairs([12,17,20]))
- Climbing Stairs in C++
- Min Cost Path
- Python Program for Min Cost Path
- Program to find to get minimum cost to climb at the top of stairs in Python?
- Min Stack in Python
- max() and min() in Python
- Program to find how many ways we can climb stairs in Python
- Use of min() and max() in Python
- Connecting Cities With Minimum Cost in Python
- Find Min-Max in heterogeneous list in Python
- Compare economic cost and accounting cost.
- Minimum Cost Tree From Leaf Values in Python
- Program to find closest dessert cost in Python
- Differentiate between direct cost and indirect cost.