What is wrong with this program?


  • 0
    K
    class Solution(object):
        stair_cost = {}
        def minCost(self, cost, total, pos):
            if pos >= len(cost):
                return total
            self.stair_cost[pos] = total
            if pos+1 not in self.stair_cost:
                total1 = self.minCost(cost, total, pos+1)
                self.stair_cost[pos+1] = total1
            else:
                total1 = self.stair_cost[pos+1]
            if pos+2 not in self.stair_cost:
                total2 = self.minCost(cost, total, pos+2)
                self.stair_cost[pos+2] = total2
            else:
                total2 = self.stair_cost[pos+2]
                
            total += cost[pos]
            total += min(total1, total2)
            return total
            
        def minCostClimbingStairs(self, cost):
            """
            :type cost: List[int]
            :rtype: int
            """
            if len(cost) == 0:
                return 0
            total = 0
            total = self.minCost(cost, total, 0)
            self.stair_cost[0] = total
            total1 = self.minCost(cost, 0, 1)
            self.stair_cost[1] = total1
            
            return min(total, total1)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.