Question 746 Min costing climbing stairs

Description of the question can be found in the following link:

 

My code

class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        if len(cost) == 2:
            return min(cost[0],cost[1])
        else:
            cost.append(0)
            TotalCostList = [0]*len(cost)
            TotalCostList[0] = cost[0]
            TotalCostList[1] = cost[1]
            for i in range(2,len(cost)):
                TotalCostList[i] = min(TotalCostList[i-1],TotalCostList[i-2])+ cost[i]
            return TotalCostList[len(cost)-1]

Explanation:

We just need to compare the cost of going from one step below or two steps below.  One extra element appended to the cost is due to the final stage we need to check. Then we just need to take the final element of the total cost list to get the minimum to go to the top.

Leave a comment