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:
- Adjacent Constraint: No two neighboring houses can have the same color
- 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code