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: Street District PlannerExample: Target 3 Districts1Red District2Blue District2Blue District1Red District✓ Exactly 3 Districts: [Red], [Blue, Blue], [Red]DP State TransitionsState: dp[house][last_color][districts] = minimum_costSame Color: dp[i][c][k] = dp[i-1][c][k] + cost (no new district)Different Color: dp[i][c][k] = dp[i-1][c'][k-1] + cost (new district)Goal: Find minimum dp[m][any_color][target]Constraint: Pre-painted houses cannot change colorCost Matrix ExampleHouse 0: Red=$1, Blue=$10House 1: Red=$10, Blue=$1House 2: Red=$10, Blue=$1House 3: Red=$1, Blue=$10House 4: Red=$5, Blue=$1Complexity: O(m × n² × target) time, O(m × n × target) space
Understanding the Visualization
1
Survey the Street
Identify which houses already have themes and which need assignment
2
Plan Districts
Choose themes for unassigned houses to create exactly the target number of districts
3
Minimize Costs
Use dynamic programming to find the assignment with minimum total renovation cost
4
Validate Solution
Ensure the final arrangement has exactly the target number of contiguous themed districts
Key Takeaway
🎯 Key Insight: Transform the exponential problem of trying all color combinations into a polynomial dynamic programming solution by tracking three essential states: house position, last color used, and current district count. This reduces complexity from O(n^k) to O(m × n² × target).
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