-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0265-paint-house-ii.py
More file actions
27 lines (22 loc) · 769 Bytes
/
0265-paint-house-ii.py
File metadata and controls
27 lines (22 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# time complexity: O(n*k^2)
# space complexity: O(1)
from typing import List
class Solution:
def minCostII(self, costs: List[List[int]]) -> int:
n = len(costs)
if n == 0:
return 0
colorSize = len(costs[0])
for house in range(1, n):
for color in range(colorSize):
best = float("inf")
for previousColor in range(colorSize):
if color == previousColor:
continue
best = min(best, costs[house - 1][previousColor])
costs[house][color] += best
return min(costs[-1])
costs = [[1, 5, 3], [2, 9, 4]]
print(Solution().minCostII(costs))
costs = [[1, 3], [2, 4]]
print(Solution().minCostII(costs))