O(N) Dynamic Programming Solution in Python


  • 0
    U
    class Solution(object):
        def minCost(self, costs):
            """
            :type costs: List[List[int]]
            :rtype: int
            """
            if not costs:
                return 0
            index = 1
            while index < len(costs):
                costs[index][0] += min(costs[index - 1][1], costs[index - 1][2])
                costs[index][1] += min(costs[index - 1][0], costs[index - 1][2])
                costs[index][2] += min(costs[index - 1][0], costs[index - 1][1])
                index += 1
            
            return min(costs[index - 1])
                
    

Log in to reply
 

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