Paint House
Problem
There is a row of n houses, and each must be painted one of three colors: red, green, or blue. Painting house i with each color has a given cost, provided as costs[i] = [red_cost, green_cost, blue_cost]. Neighboring houses must not share a color. Return the minimum total cost to paint every house.
costshas up to a hundred houses, each with a cost between 1 and about 20 for every color.
Examples
costs = [[8, 4, 15], [9, 12, 3], [6, 5, 18]]→12. Paint house 0 green (4), house 1 blue (3), house 2 green (5).costs = [[5, 9, 1]]→1. A single house takes its cheapest color.costs = [[2, 3, 4], [2, 3, 4]]→5. Greedy would pick the 2 for both houses, which is illegal; the best legal option is 2 then 3.
Hints
- Picking the cheapest color for each house independently fails because of the neighbor rule. What single piece of information about the previous house affects the current choice?
- For each house, compute the best total for each of the three possible colors of that house, using the best totals of the other two colors on the previous house.
- State machine DP with three states per house:
best[i][c] = costs[i][c] + min(best[i-1][c'] for c' != c).
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.