Paint House II - Problem
Paint House II is a classic dynamic programming problem where you need to minimize the cost of painting houses while following a constraint.
You're given
The cost of painting each house with a specific color is given in an
Goal: Find the minimum total cost to paint all houses while satisfying the adjacency constraint.
Example: If
You're given
n houses arranged in a row and k different colors. Each house can be painted with any of the k colors, but here's the catch: no two adjacent houses can have the same color.The cost of painting each house with a specific color is given in an
n Γ k matrix called costs, where costs[i][j] represents the cost of painting house i with color j.Goal: Find the minimum total cost to paint all houses while satisfying the adjacency constraint.
Example: If
costs = [[17,2,17],[16,16,5],[14,3,19]], you have 3 houses and 3 colors. The optimal solution might be: paint house 0 with color 1 (cost 2), house 1 with color 2 (cost 5), and house 2 with color 1 (cost 3), for a total cost of 10. Input & Output
example_1.py β Basic Case
$
Input:
costs = [[17,2,17],[16,16,5],[14,3,19]]
βΊ
Output:
10
π‘ Note:
Paint house 0 with color 1 (cost=2), house 1 with color 2 (cost=5), house 2 with color 1 (cost=3). Total cost = 2+5+3 = 10. This is optimal since adjacent houses have different colors and total cost is minimized.
example_2.py β Single House
$
Input:
costs = [[7,6,2]]
βΊ
Output:
2
π‘ Note:
With only one house, we simply choose the cheapest color available, which is color 2 with cost 2. No adjacency constraints apply.
example_3.py β Two Colors Only
$
Input:
costs = [[1,5],[2,3],[4,1]]
βΊ
Output:
5
π‘ Note:
With only 2 colors, we alternate: House 0βColor 0 (cost=1), House 1βColor 1 (cost=3), House 2βColor 0 (cost=4). Total = 1+3+1 = 5. Alternative path gives 5+2+4 = 11, so first path is optimal.
Visualization
Tap to expand
Understanding the Visualization
1
Analyze First House
Look at all color options for the first house and identify the cheapest and second cheapest
2
Move to Next House
For each color option, use the minimum cost from previous house if it's a different color, otherwise use second minimum
3
Track Best Options
Keep track of the minimum and second minimum costs for current house to use for next house
4
Continue Pattern
Repeat this process for all houses, always ensuring adjacent houses have different colors
Key Takeaway
π― Key Insight: We only need to track the minimum and second minimum costs from the previous step, not the entire history. This reduces space complexity to O(1) while maintaining optimal O(nΓk) time complexity.
Time & Space Complexity
Time Complexity
O(n*kΒ²)
For each of n houses, we consider k colors, and for each color we scan k-1 previous colors
β Linear Growth
Space Complexity
O(n*k)
DP table stores minimum cost for each house-color combination
β‘ Linearithmic Space
Constraints
- costs.length == n
- costs[i].length == k
- 1 β€ n β€ 100
- 2 β€ k β€ 20
- 1 β€ costs[i][j] β€ 20
- Follow-up: Could you solve it in O(nk) time and O(1) space?
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code