Paint House IV - Problem

You're tasked with painting houses in a neighborhood to create the most beautiful arrangement possible while minimizing costs. Given n houses (where n is even) arranged in a straight line, you need to paint each house with one of three colors (Red, Green, or Blue).

The neighborhood will look beautiful if two strict conditions are met:

  1. Adjacent Constraint: No two neighboring houses can have the same color
  2. Mirror Constraint: Houses that are equidistant from the ends must have different colors

For example, with 6 houses: houses at positions (0,5), (1,4), and (2,3) are equidistant pairs and must have different colors.

Each house i has different painting costs for each color, given in cost[i][j] where j represents the color (0=Red, 1=Green, 2=Blue).

Goal: Find the minimum cost to paint all houses while satisfying both beauty conditions.

Input & Output

example_1.py — Basic Case
$ Input: cost = [[1,5,3],[2,9,4]]
Output: 5
💡 Note: Paint house 0 with color 0 (cost=1) and house 1 with color 2 (cost=4). Total cost = 1+4 = 5. Adjacent constraint: 0≠2 ✓, Mirror constraint: 0≠2 ✓
example_2.py — Four Houses
$ Input: cost = [[1,2,3],[1,4,6],[2,3,1],[4,2,1]]
Output: 6
💡 Note: Optimal solution: colors [0,1,2,1] with costs [1,2,1,2]. Adjacent constraints: 0≠1✓, 1≠2✓, 2≠1✓. Mirror constraints: houses(0,3): 0≠1✓, houses(1,2): 1≠2✓. Total cost = 1+2+1+2 = 6.
example_3.py — Minimum Case
$ Input: cost = [[5,8,6],[19,14,13]]
Output: 18
💡 Note: Two houses: Paint house 0 with color 0 (cost=5) and house 1 with color 2 (cost=13). Adjacent: 0≠2 ✓, Mirror: 0≠2 ✓. Total = 5+13 = 18.

Constraints

  • n == cost.length
  • 2 ≤ n ≤ 20
  • n is even
  • cost[i].length == 3
  • 1 ≤ cost[i][j] ≤ 20

Visualization

Tap to expand
Paint House IV - Dynamic Programming INPUT 2 Houses (n=2, even) House 0 House 1 Mirror Pair (0,1) Cost Matrix House Red Green Blue 0 1 5 3 1 2 9 4 Constraints: 1. Adjacent: diff colors 2. Mirror: diff colors cost=[[1,5,3],[2,9,4]] ALGORITHM STEPS 1 Define DP State dp[i][c1][c2] = min cost for house i with color c1, mirror c2 2 Process Mirror Pairs Pair: (house 0, house 1) Must have different colors 3 Enumerate Valid Combos Try all (c1,c2) where c1!=c2 Valid Color Combinations: H0:R + H1:G = 1+9 = 10 H0:R + H1:B = 1+4 = 5 MIN! H0:G + H1:R = 5+2 = 7 H0:G + H1:B = 5+4 = 9 H0:B + H1:R = 3+2 = 5 MIN! H0:B + H1:G = 3+9 = 12 4 Select Minimum min(10,5,7,9,5,12) = 5 Two optimal solutions exist FINAL RESULT Optimal Solution 1: RED Cost: 1 BLUE Cost: 4 Total: 1 + 4 = 5 Optimal Solution 2: BLUE Cost: 3 RED Cost: 2 Total: 3 + 2 = 5 OUTPUT 5 OK - Both constraints satisfied! Key Insight: The Mirror Constraint creates pairs of houses that must have different colors. With n=2, houses 0 and 1 form a mirror pair AND are adjacent. This means both constraints apply to the same pair, reducing the problem to: pick two different colors for houses 0 and 1 that minimize total cost. DP handles larger cases. TutorialsPoint - Paint House IV | Dynamic Programming Approach
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 24
52.3K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen