Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def minCost(self, numHouses: int, costsToPaint: List[List[int]]) -> int:
- middleIndex = (numHouses / 2)
- calculations = [[] for i in range(numHouses)]
- for i in range(numHouses):
- for j in range(4):
- calculations[i].append([])
- for k in range(4):
- calculations[i][j].append(-1)
- def calculate(i, previousHouseColor, previousMirroredColor):
- if i >= middleIndex: return 0
- calculatedAlready = (calculations[i][previousHouseColor][previousMirroredColor] != -1)
- if calculatedAlready: return calculations[i][previousHouseColor][previousMirroredColor]
- minResult = math.inf
- # color1 => current (i-th) house
- for color1 in range(3):
- if color1 == previousHouseColor: continue
- # color2 => mirrored house
- for color2 in range(3):
- if color2 == previousMirroredColor or color2 == color1: continue
- costToPaint_ith = costsToPaint[i][color1]
- mirroredIndex = (numHouses - 1 - i)
- costToPaint_mirrored = costsToPaint[mirroredIndex][color2]
- costToPaint_next = calculate(i + 1, color1, color2)
- resultHere = (costToPaint_ith + costToPaint_mirrored + costToPaint_next)
- minResult = min(minResult, resultHere)
- calculations[i][previousHouseColor][previousMirroredColor] = minResult
- return minResult
- return calculate(0, 3, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement