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
House Painting OptimizationHouse 0House 1House 2Cost: 2Cost: 5Cost: 3Color OptionsRed: 17Green: 2 ✓Blue: 17≠ Green≠ BlueDP Algorithm1. Track min & 2nd min costs2. For each house & color:• Use prev min (diff color)• Use prev 2nd min (same)3. Update min & 2nd min4. Continue to next houseResult: Total Cost = 10
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.
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