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
A neighborhood is a maximal group of consecutive houses painted with the same color. For example:
Input:
•
•
•
Output: Minimum cost to achieve exactly
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 neighborhoodsOutput: 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
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).
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code