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 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.

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?

Visualization

Tap to expand
Paint House II - Dynamic Programming INPUT n=3 houses, k=3 colors House 0 House 1 House 2 Cost Matrix Red Green Blue H0 H1 H2 17 2 17 16 16 5 14 3 19 Constraint: Adjacent houses must have different colors ALGORITHM STEPS 1 Initialize DP dp[i][j] = min cost to paint house i with color j 2 Base Case dp[0][j] = costs[0][j] First row: [17, 2, 17] 3 Transition For each house i, color j: dp[i][j] = costs[i][j] + min(dp[i-1][m]) where m!=j 4 Final Answer min(dp[n-1][j]) for all j DP Table: 17 2 17 19 18 7 21 10 26 FINAL RESULT Optimal Painting Sequence: House 0 Green: $2 House 1 Blue: $5 House 2 Green: $3 Cost Breakdown: House 0 (Green): 2 House 1 (Blue): 5 House 2 (Green): 3 Total: 2 + 5 + 3 Output: 10 [OK] Minimum cost found! Key Insight: Track two minimums per row: 1st minimum and 2nd minimum. For each color j, use 1st min if j != argmin, else use 2nd min. This optimizes from O(nk^2) to O(nk). The DP recurrence ensures no adjacent houses share colors by excluding current color from previous row's minimum. TutorialsPoint - Paint House II | Dynamic Programming Approach
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
58.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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