Quick Overview

Compute the minimum cost to paint a row of houses with k colors so that no two adjacent houses share a color. The problem tests dynamic programming and how to keep the per-house work small as the number of colors grows.

Minimum Cost to Paint a Row of Houses With k Colors and No Two Neighbors Matching

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

There is a row of `n` houses, and each house can be painted with one of `k` colors. The cost of painting a house depends on both the house and the color. You have to paint all the houses so that no two adjacent houses have the same color. The costs are given as an `n x k` matrix `costs`, where `costs[i][j]` is the cost of painting house `i` with color `j`. Return the minimum total cost to paint all the houses. ### Function Signature ```python def min_cost(costs: list[list[int]]) -> int: ``` ### Rules - Colors are numbered `0` to `k - 1`, one per column of `costs`. - Only houses `i` and `i + 1` count as adjacent. Houses that are further apart may share a color. - Return only the minimum total cost, not the colors chosen. ### Constraints - `n == len(costs)` - `k == len(costs[i])` for every `i` - `1 <= n <= 100` - `2 <= k <= 20` - `1 <= costs[i][j] <= 20` ### Examples ```text Input: costs = [[1, 5, 3], [2, 9, 4]] Output: 5 ``` Paint house 0 with color 0 (cost 1) and house 1 with color 2 (cost 4), for a total of 1 + 4 = 5. Color 2 then color 0 (3 + 2) also costs 5, and both houses cannot take their cheapest color 0. ```text Input: costs = [[1, 3], [2, 4]] Output: 5 ``` With two colors the houses must alternate. Color 0 then color 1 costs 1 + 4 = 5, and color 1 then color 0 costs 3 + 2 = 5. ```text Input: costs = [[2, 5, 7, 4], [1, 6, 3, 8], [2, 4, 9, 6]] Output: 7 ``` Color 0 is the cheapest for every house. Houses 0 and 2 are not adjacent, so both can take color 0 (2 + 2), and house 1 takes color 2 at 3, for 7. Giving house 1 color 0 instead would push houses 0 and 2 onto colors costing 4 each, for 4 + 1 + 4 = 9.

Overview: Compute the minimum cost to paint a row of houses with k colors so that no two adjacent houses share a color. The problem tests dynamic programming and how to keep the per-house work small as the number of colors grows.

A street has a row of `n` houses, and each house must be painted with one of `k` colors. The price of painting a house depends on both the house and the color. You are given an `n x k` integer matrix `costs`, where `costs[i][j]` is the cost of painting house `i` with color `j`. Paint every house so that no two adjacent houses have the same color, and return the minimum total cost. Rules: - Colors are numbered `0` to `k - 1`, one per column of `costs`. - Only houses `i` and `i + 1` are adjacent. Houses that are farther apart may share a color. - Return the minimum total cost as a single integer. The colors chosen are not part of the output, so when several colorings tie at the minimum the answer is that one number. Implement `min_cost(costs)`. Constraints: - `n == len(costs)` - `k == len(costs[i])` for every `i` - `1 <= n <= 100` - `2 <= k <= 20` - `1 <= costs[i][j] <= 20` - The answer is at most 2000, so it never exceeds `2^31 - 1` and a 32-bit `int` holds it in every language. Example 1: Input: costs = [[1, 5, 3], [2, 9, 4]] Output: 5 Explanation: Paint house 0 with color 0 (cost 1) and house 1 with color 2 (cost 4), for 1 + 4 = 5. Color 2 then color 0 (3 + 2) also costs 5. The two houses are adjacent, so they cannot both take color 0, their cheapest color. Example 2: Input: costs = [[2, 5, 7, 4], [1, 6, 3, 8], [2, 4, 9, 6]] Output: 7 Explanation: Color 0 is the cheapest for every house. Houses 0 and 2 are not adjacent, so both can take color 0 (2 + 2), and house 1 takes color 2 at 3, for a total of 7. Giving house 1 color 0 instead would push houses 0 and 2 onto colors costing 4 each, for 4 + 1 + 4 = 9.

Constraints

  • n == len(costs)
  • k == len(costs[i]) for every i
  • 1 <= n <= 100
  • 2 <= k <= 20
  • 1 <= costs[i][j] <= 20
  • The answer is at most 2000, so it fits in a 32-bit signed integer

Examples

Input: ([[1, 5, 3], [2, 9, 4]],)

Expected Output: 5

Explanation: Source example 1: color 0 then color 2 (1 + 4) and color 2 then color 0 (3 + 2) both cost 5; the neighbors cannot both take color 0.

Input: ([[1, 3], [2, 4]],)

Expected Output: 5

Explanation: Source example 2: two colors force alternation, and both alternations cost 5.

Hints

  1. Only houses i and i + 1 constrain each other. When you choose the color of house i, what do you need to know about the houses before it?
  2. Houses that are not neighbors may share a color, so the cheapest color can be used many times in one optimal painting.
  3. A row can have its cheapest cost in two different colors. Make sure a tie like that does not stop a neighbor from using the cheap price.

Cheapest Way to Paint a Row of Houses With No Matching Neighbors

hardGeneralSoftware Engineer

A street has a row of n houses, and each house must be painted with one of k colors. The price of painting a house depends on both the house and the color. You are given an n x k integer matrix costs, where costs[i][j] is the cost of painting house i with color j.

Paint every house so that no two adjacent houses have the same color, and return the minimum total cost.

Rules:

  • Colors are numbered 0 to k - 1, one per column of costs.
  • Only houses i and i + 1 are adjacent. Houses that are farther apart may share a color.
  • Return the minimum total cost as a single integer. The colors chosen are not part of the output, so when several colorings tie at the minimum the answer is that one number.

Implement min_cost(costs).

Constraints:

  • n == len(costs)
  • k == len(costs[i]) for every i
  • 1 <= n <= 100
  • 2 <= k <= 20
  • 1 <= costs[i][j] <= 20
  • The answer is at most 2000, so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Example 1: Input: costs = [[1, 5, 3], [2, 9, 4]] Output: 5 Explanation: Paint house 0 with color 0 (cost 1) and house 1 with color 2 (cost 4), for 1 + 4 = 5. Color 2 then color 0 (3 + 2) also costs 5. The two houses are adjacent, so they cannot both take color 0, their cheapest color.

Example 2: Input: costs = [[2, 5, 7, 4], [1, 6, 3, 8], [2, 4, 9, 6]] Output: 7 Explanation: Color 0 is the cheapest for every house. Houses 0 and 2 are not adjacent, so both can take color 0 (2 + 2), and house 1 takes color 2 at 3, for a total of 7. Giving house 1 color 0 instead would push houses 0 and 2 onto colors costing 4 each, for 4 + 1 + 4 = 9.

Examples

Example 1
Input
([[1, 5, 3], [2, 9, 4]],)
Output
5
Notes
Source example 1: color 0 then color 2 (1 + 4) and color 2 then color 0 (3 + 2) both cost 5; the neighbors cannot both take color 0.
Example 2
Input
([[1, 3], [2, 4]],)
Output
5
Notes
Source example 2: two colors force alternation, and both alternations cost 5.

Constraints

  • •n == len(costs)
  • •k == len(costs[i]) for every i
  • •1 <= n <= 100
  • •2 <= k <= 20
  • •1 <= costs[i][j] <= 20
  • •The answer is at most 2000, so it fits in a 32-bit signed integer

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_cost(costs: list[list[int]]) -> int:
Sample call
min_cost([[1, 5, 3], [2, 9, 4]])
Language
Case 1
Input
([[1, 5, 3], [2, 9, 4]],)
Expected
5
Case 2
Input
([[1, 3], [2, 4]],)
Expected
5

Browse More Questions

Show the approach

Approach

Let best(i, j) be the minimum cost to paint houses 0 to i when house i gets color j. Then best(0, j) = costs[0][j], and for i >= 1, best(i, j) = costs[i][j] + the minimum of best(i - 1, c) over all colors c != j. The answer is the minimum of best(n - 1, j) over all j. The recurrence is exact because the only rule links consecutive houses: a valid coloring of houses 0 to i that ends in color j is a valid coloring of houses 0 to i - 1 ending in some color c != j, followed by house i, and every such combination is valid. Houses farther apart are unconstrained, so a color may repeat as long as it does not repeat on neighbors.

Scanning every c for each j costs O(k) per cell. To get O(1) per cell, keep three facts about the previous row: its smallest value, the column holding it, and the second smallest value over the remaining columns. For column j the cheapest compatible previous cost is the smallest value, unless j is that column, in which case it is the second smallest. When the minimum is tied across two columns, the second smallest equals the smallest, so a tie never forces a more expensive choice. Only the previous row is kept, so extra space is O(k).

Edge cases: with one house the answer is the smallest value in its row; with k = 2 the recurrence allows only the two alternating colorings; every cost is between 1 and 20, so the total is at most 2000 and fits in a 32-bit integer.

Time complexity:
O(n * k)
Space complexity:
O(k)