Paint House III - Problem
Paint House III is a challenging optimization problem where you need to strategically paint houses to create a specific number of neighborhoods while minimizing cost.

You have a row of m houses, each needing to be painted with one of n colors (labeled 1 to n). Some houses are already painted and cannot be repainted. Your goal is to paint the remaining houses such that you create exactly target neighborhoods with minimum cost.

A neighborhood is a maximal group of consecutive houses painted with the same color. For example: [1,2,2,3,3,2,1,1] has 5 neighborhoods: [1], [2,2], [3,3], [2], [1,1].

Input:
houses[i]: color of house i (0 if unpainted)
cost[i][j]: cost to paint house i with color j+1
target: desired number of neighborhoods

Output: Minimum cost to achieve exactly target neighborhoods, or -1 if impossible.

Input & Output

example_1.py — Basic Case
$ 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
💡 Note: Paint houses as [2,1,1,1,2] to create neighborhoods [2], [1,1,1], [2]. Total cost = 10+1+1+1+1 = 14. But optimal is [1,2,2,1,2] with cost 1+1+1+10+1 = 14. Actually optimal is [2,1,1,2,1] = 10+1+1+10+5 = 27. The actual optimal solution gives cost 9.
example_2.py — Pre-painted Houses
$ 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
💡 Note: Houses are [2,2,1,2,1] after painting first and last houses. This creates neighborhoods [2,2], [1], [2], [1] = 4 neighborhoods, not 3. Need to find optimal way to achieve exactly 3 neighborhoods.
example_3.py — Impossible Case
$ Input: houses = [3,1,2,3], cost = [], m = 4, n = 3, target = 3
Output: -1
💡 Note: Houses are already painted as [3,1,2,3] creating 4 neighborhoods: [3], [1], [2], [3]. Cannot change to 3 neighborhoods since no houses can be repainted.

Constraints

  • m == houses.length == cost.length
  • n == cost[i].length
  • 1 ≤ m ≤ 100
  • 1 ≤ n ≤ 20
  • 1 ≤ target ≤ m
  • 0 ≤ houses[i] ≤ n
  • 1 ≤ cost[i][j] ≤ 104
  • All houses must be painted with exactly one color

Visualization

Tap to expand
Paint House III - Dynamic Programming INPUT 5 Houses (all unpainted) 0 0 0 0 0 Cost Matrix (Color 1 | Color 2) House 0: [ 1, 10] House 1: [10, 1] House 2: [10, 1] House 3: [ 1, 10] House 4: [ 5, 1] m = 5 (houses) n = 2 (colors) target = 3 neighborhoods ALGORITHM STEPS 1 Define DP State dp[i][j][k] = min cost for house i, color j, k groups 2 Transition Logic Same color: k unchanged Diff color: k increases by 1 3 Process Each House If painted: use existing color If not: try all colors + costs 4 Find Minimum Check all dp[m-1][*][target] Return min or -1 if impossible Optimal Path Found: H0:C1(1) H1:C2(1) H2:C2(0) H3:C1(1) H4:C2(1) Groups: [1],[2,2],[1,2] Cost: 1+1+0+1+1 = 9 FINAL RESULT Optimal Coloring C1 C2 C2 C1 C2 3 Neighborhoods Formed: [1] [2,2] [1,2] Cost Breakdown House 0 (C1): 1 House 1 (C2): 1 House 2 (C2): 1 House 3 (C1): 1 House 4 (C2): 1 Total: 5 OUTPUT 9 OK - Minimum cost achieved! Key Insight: The 3D DP state dp[house][color][neighborhoods] tracks minimum cost to paint houses 0 to i, ending with a specific color and forming exactly k neighborhoods. Transitioning between colors creates a new neighborhood (k+1), while same color continues current neighborhood (k unchanged). TutorialsPoint - Paint House III | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
31.2K Views
Medium-High Frequency
~25 min Avg. Time
1.3K 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