Simple and Concise 4-line Python solution for general case

  • 0

    At the end of the ith loop, arr[j] represents the minimum cost of painting House 0 to House i such that House i is painted with color j and House 0 to House i-1 is painted with any combination of colors without breaking the rule.

            arr = [0]*len(costs[0]) if (costs and costs[0]) else [0]
            for i in range(len(costs)):
                arr = [costs[i][j] + min(arr[:j] + arr[j+1:]) for j in range(len(costs[0]))]
            return min(arr)

Log in to reply

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