Paint House III Problem
Description
LeetCode Problem 1473.
There is a row of m houses in a small city, each house must be painted with one of the n colors (labeled from 1 to n), some houses that have been painted last summer should not be painted again.
A neighborhood is a maximal group of continuous houses that are painted with the same color.
- For example:
houses = [1,2,2,3,3,2,1,1]contains5neighborhoods[{1}, {2,2}, {3,3}, {2}, {1,1}].
Given an array houses, an m x n matrix cost and an integer target where:
houses[i]: is the color of the housei, and0if the house is not painted yet.cost[i][j]: is the cost of paint the houseiwith the colorj + 1.
Return the minimum cost of painting all the remaining houses in such a way that there are exactly target neighborhoods. If it is not possible, return -1.
Example 1:
Input: houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
Output: 9
Explanation: Paint houses of this way [1,2,2,1,1]
This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}].
Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9.
Example 2:
Input: houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
Output: 11
Explanation: Some houses are already painted, Paint the houses of this way [2,2,1,2,2]
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
Cost of paint the first and last house (10 + 1) = 11.
Example 3:
Input: houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3
Output: -1
Explanation: Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3.
Constraints:
m == houses.length == cost.lengthn == cost[i].length1 <= m <= 1001 <= n <= 201 <= target <= m0 <= houses[i] <= n1 <= cost[i][j] <= 10^4
Solution
Approach: Dynamic Programming on Color and Neighborhood Count
Go through the houses from left to right. To decide what happens at the next house, we only need to know the color of the current one and how many neighborhoods have formed so far, so let dp[c][k] be the lowest cost to paint the houses so far with the last one in color c and exactly k neighborhoods.
For house i and a color c it may have (any color if houses[i] == 0, otherwise only its own color, which costs nothing):
- If the previous house also has color
c, the neighborhood continues: we come fromdp[c][k]. - If it has another color, house
istarts neighborhoodk: we come from the cheapestdp[c'][k - 1]withc' != c.
Add the paint cost (cost[i][c] or 0) to the cheaper of the two. On the first house, dp[c][1] is just its paint cost. The answer is the smallest dp[c][target] after the last house, or -1 if every entry is unreachable (example 3, where the fixed colors already give 4 neighborhoods).
Trying every previous color costs O(n) per state. To avoid that, before each house compute, for every k, the smallest and second-smallest dp[.][k] and which color gives the smallest. The best "other color" is then the smallest value, unless it belongs to c itself, in which case it is the second smallest.
Complexity: O(m · n · target) time and O(n · target) extra space, since only the previous house's table is kept.
class Solution:
def minCost(self, houses: List[int], cost: List[List[int]], m: int, n: int, target: int) -> int:
INF = 10**9 # far above any real cost (at most 100 * 10^4)
# prev[c][k]: cheapest cost for the houses so far, the last one painted with
# color c, forming exactly k neighborhoods.
prev = [[INF] * (target + 1) for _ in range(n)]
for i in range(m):
# For each k, the two smallest prev[.][k] and the color of the smallest, so
# "best previous house of a different color" is O(1).
best = []
for k in range(target + 1):
b1, b1_color, b2 = INF, -1, INF
for c in range(n):
v = prev[c][k]
if v < b1:
b1, b1_color, b2 = v, c, b1
elif v < b2:
b2 = v
best.append((b1, b1_color, b2))
cur = [[INF] * (target + 1) for _ in range(n)]
for c in range(n):
if houses[i] != 0 and houses[i] != c + 1:
continue # a painted house keeps its color
paint = cost[i][c] if houses[i] == 0 else 0
if i == 0:
cur[c][1] = paint
continue
for k in range(1, min(target, i + 1) + 1):
b1, b1_color, b2 = best[k - 1]
# same color as the previous house: still k neighborhoods;
# a different color: the previous houses formed k - 1
low = min(prev[c][k], b2 if b1_color == c else b1)
if low < INF:
cur[c][k] = low + paint
prev = cur
answer = min(prev[c][target] for c in range(n))
return -1 if answer >= INF else answer