Paint House
Example
Input: [[14,2,11],[11,14,5],[14,3,10]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue. Minimum cost: 2 + 5 + 3 = 10.Input: [[1,2,3],[1,4,6]]
Output: 3Notice
class Solution:
"""
@param costs: n x 3 cost matrix
@return: An integer, the minimum cost to paint all houses
"""
def minCost(self, costs):
# write your code here
if not costs:
return 0
n = len(costs)
for i in range(1,n):
costs[i][0] = costs[i][0] + min(costs[i-1][1],costs[i-1][2])
costs[i][1] = costs[i][1] + min(costs[i-1][0],costs[i-1][2])
costs[i][2] = costs[i][2] + min(costs[i-1][0],costs[i-1][1])
return min(costs[n-1])
Last updated