Paint House

Similar Problems:

- Paint Fence
- CheatSheet: Leetcode For Code Interview
- CheatSheet: Common Code Problems & Follow-ups
- Tag: #dynamicprogramming, #paintfence

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by a n x 3 cost matrix.

For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Note:

All costs are positive integers.

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution: dp

## https://code.dennyzhang.com/paint-house ## Basic Ideas: dynamic programming ## ## For each house, we need to choose one color ## From left to right, loop the houses ## ## Complexity: Time O(n), Space O(1) class Solution: def minCost(self, costs: List[List[int]]) -> int: r, b, g = 0, 0, 0 for i in range(len(costs)): [rc, bc, gc] = costs[i] rc += min(b, g) bc += min(r, g) gc += min(r, b) r, b, g = rc, bc, gc return min(r, b, g)

## https://code.dennyzhang.com/paint-house ## Basic Ideas: Dyanmic programming ## For house after house1, it can only have 3 possilities. ## ## Complexity: Time O(n), Space O(1) class Solution: def minCost(self, costs): """ :type costs: List[List[int]] :rtype: int """ length = len(costs) if length == 0: return 0 dp = [None]*3 # caculate house I dp = costs[0] # caculate the following house for i in range(1, length): l = [None]*3 for j in range(0, 3): l[j] = costs[i][j] + min(dp[(j+1)%3], dp[(j+2)%3]) dp = l return min(dp)