This problem asks for the minimum cost to paint n
houses with k
colors, ensuring no adjacent houses have the same color. We can solve this efficiently using dynamic programming.
The dynamic programming approach builds a solution iteratively. We maintain an array f
(or a similar data structure) to store the minimum cost to paint the houses up to a certain point.
Initialization: Initialize f
with the costs of painting the first house with each color (costs[0]
).
Iteration: For each subsequent house (i
from 1 to n-1
):
g
(or modify f
in place).j
:
t
to paint the previous houses (i-1
) with a color different from j
. This is the minimum value in f
, excluding f[j]
.g[j]
as costs[i][j] + t
. This represents the minimum cost to paint house i
with color j
, given the optimal choices for previous houses.g
to f
for the next iteration.Result: After iterating through all houses, the minimum value in f
represents the overall minimum cost to paint all houses.
f
of size k
to store the minimum costs.The crucial optimization is to avoid the inner loop that searches for t
(the minimum cost excluding the current color). Instead, we can track the minimum and second minimum costs among the previous houses in constant time.
Here's how the optimized algorithm works:
Initialization: Initialize min1
(minimum cost), min2
(second minimum cost), and min1Idx
(index of minimum cost) for the first house.
Iteration: For each subsequent house:
min1Idx
, update the cost using min1
.min2
.min1
, min2
, and min1Idx
based on the updated costs.class Solution:
def minCostII(self, costs: List[List[int]]) -> int:
n = len(costs)
if n == 0:
return 0
k = len(costs[0])
if k == 0:
return 0
min1, min2, min1Idx = float('inf'), float('inf'), -1
for j in range(k):
if costs[0][j] < min1:
min2 = min1
min1 = costs[0][j]
min1Idx = j
elif costs[0][j] < min2:
min2 = costs[0][j]
for i in range(1, n):
newMin1, newMin2, newMin1Idx = float('inf'), float('inf'), -1
for j in range(k):
cost = costs[i][j]
if j == min1Idx:
cost += min2
else:
cost += min1
if cost < newMin1:
newMin2 = newMin1
newMin1 = cost
newMin1Idx = j
elif cost < newMin2:
newMin2 = cost
min1, min2, min1Idx = newMin1, newMin2, newMin1Idx
return min1
The other languages (Java, C++, Go) can be similarly optimized using this approach, reducing the time complexity to O(n*k). The space complexity remains O(1) as we only track a few variables instead of an entire array.