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 −
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]))