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.

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.

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

n
2n
βœ“ Linear Growth
Space Complexity
O(n*k)

DP table stores minimum cost for each house-color combination

n
2n
⚑ 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?
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