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 [1,0,2,1] with costs [2,1,1,2]. Adjacent: 1≠0, 0≠2, 2≠1 ✓. Mirror: houses(0,3): 1≠1 ✗. Need to try [1,2,0,2]: costs [2,6,2,2]=12. Better: [0,2,1,2]: costs [1,6,3,2]=12. Optimal: [2,1,0,1]: costs [3,4,2,2]=11. Actually optimal is [1,0,2,0]: costs [2,1,1,4]=8. But mirror (1≠0) ✓, adjacent ✓.
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
Beautiful House PaintingThe Symmetric StreetH0Cost: 1H1Cost: 4H2Cost: 2H3Cost: 2Mirror PairsConstraint Validation:✓ Adjacent: Red≠Green, Green≠Blue, Blue≠Green✓ Mirror: (H0,H3): Red≠Green, (H1,H2): Green≠Blue💰 Total Cost: 1 + 4 + 2 + 2 = 9
Understanding the Visualization
1
Set Up The Street
Visualize houses in a line with mirror positions clearly marked
2
Apply Local Rules
Ensure no two adjacent houses have the same color
3
Enforce Symmetry
Make sure houses equidistant from ends have different colors
4
Optimize Costs
Among all valid arrangements, choose the one with minimum total cost
Key Takeaway
🎯 Key Insight: The problem combines local constraints (adjacent houses) with global symmetry requirements (mirror positions), making it ideal for dynamic programming where we build valid solutions incrementally while tracking both types of constraints.
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