Paint House IV Problem
Description
LeetCode Problem 3429.
You are given an even integer n representing the number of houses arranged in a straight line, and a 2D array cost of size n x 3, where cost[i][j] represents the cost of painting house i with color j + 1.
The houses will look beautiful if they satisfy the following conditions:
- No two adjacent houses are painted the same color.
- Houses equidistant from the ends of the row are not painted the same color. For example, if
n = 6, houses at positions(0, 5),(1, 4), and(2, 3)are considered equidistant.
Return the minimum cost to paint the houses such that they look beautiful.
Example 1:
Input: n = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]]
Output: 9
Explanation:
The optimal painting sequence is [1, 2, 3, 2] with corresponding costs [3, 2, 1, 3]. This satisfies the following conditions:
- No adjacent houses have the same color.
- Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color (1 != 2).
- Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color (2 != 3).
The minimum cost to paint the houses so that they look beautiful is 3 + 2 + 1 + 3 = 9.
Example 2:
Input: n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]]
Output: 18
Explanation:
The optimal painting sequence is [1, 3, 2, 3, 1, 2] with corresponding costs [2, 8, 1, 2, 3, 2]. This satisfies the following conditions:
- No adjacent houses have the same color.
- Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color (1 != 2).
- Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color (3 != 1).
- Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color (2 != 3).
The minimum cost to paint the houses so that they look beautiful is 2 + 8 + 1 + 2 + 3 + 2 = 18.
Constraints:
2 <= n <= 10^5nis even.cost.length == ncost[i].length == 30 <= cost[i][j] <= 10^5
Solution
Approach: DP Over Pairs of Houses
The second rule ties house i to house n - 1 - i, so process these pairs from the outside in. Let dp[a][b] be the cheapest cost so far when the left house of the latest pair has color a and its right house has color b, with a != b.
For the next pair, try every (a, b) with a != b. The new left house is adjacent to the previous left house and the new right house to the previous right house, so we need pa != a and pb != b for the previous colors (pa, pb). Take the cheapest such previous state and add the two painting costs. The two middle houses form the last pair, and they are adjacent, which the rule a != b already covers. The answer is the minimum over the last dp.
Complexity: O(n) time (a constant 3 x 3 x 3 x 3 work per pair) and O(1) space.
class Solution:
def minCost(self, n: int, cost: List[List[int]]) -> int:
inf = float("inf")
# Process the pairs (i, n-1-i) from the outside in. dp[a][b] = cheapest cost so far when the
# left house of the latest pair has color a and its right house has color b (a != b).
dp = None
for i in range(n // 2):
j = n - 1 - i
cur = [[inf] * 3 for _ in range(3)]
for a in range(3):
for b in range(3):
if a == b:
continue
best = 0
if dp is not None:
best = min(dp[pa][pb] for pa in range(3) for pb in range(3) if pa != a and pb != b)
cur[a][b] = best + cost[i][a] + cost[j][b]
dp = cur
return min(min(row) for row in dp)