# 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 −

## Example

Live Demo

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

## Input

[12,17,20]

## Output

17