There is a row of n houses, where each house can be painted one of three colors: red, blue, or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by an n x 3 cost matrix costs.
costs[0][0] is the cost of painting house 0 with the color red; costs[1][2] is the cost of painting house 1 with color green, and so on...Return the minimum cost to paint all houses.
Example 1:
Input: costs = [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
Minimum cost: 2 + 5 + 3 = 10.
Example 2:
Input: costs = [[7,6,2]]
Output: 2
Constraints:
costs.length == ncosts[i].length == 31 <= n <= 1001 <= costs[i][j] <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a row of houses and a palette of different paint colors, and you want to paint every house while following the rule that no two neighboring houses can be painted the same color. The brute force approach works by testing every single possible color combination from the first house to the last, calculating the total cost for each complete combination, and finally picking the one that cost the least money.
Here's how the algorithm would work step-by-step:
def min_cost(costs):
if not costs:
return 0
def calculate_cost(house_index, current_color):
current_cost = costs[house_index][current_color]
# Base case to stop recursion once we evaluate the final house in our row
if house_index == len(costs) - 1:
return current_cost
minimum_remaining_cost = float('inf')
# Iterate through all available colors to find the optimal valid path forward
for next_color in range(3):
if next_color != current_color:
subsequent_cost = calculate_cost(house_index + 1, next_color)
if subsequent_cost < minimum_remaining_cost:
minimum_remaining_cost = subsequent_cost
return current_cost + minimum_remaining_cost
total_minimum_cost = float('inf')
# Test every valid starting color for the first house to initiate our search
for starting_color in range(3):
candidate_cost = calculate_cost(0, starting_color)
if candidate_cost < total_minimum_cost:
total_minimum_cost = candidate_cost
# Return the ultimate minimum cost found across all explored branches
return total_minimum_costInstead of testing every possible color combination for every house from the start, we can work our way forward step by step. By keeping track of the cheapest running totals for each color as we move from one house to the next, we guarantee we find the absolute cheapest overall cost without redoing any work.
Here's how the algorithm would work step-by-step:
def min_cost(costs):
if not costs:
return 0
# We modify the input matrix in place to save memory during traversal.
for house_index in range(1, len(costs)):
costs[house_index][0] += min(costs[house_index - 1][1], costs[house_index - 1][2])
costs[house_index][1] += min(costs[house_index - 1][0], costs[house_index - 1][2])
costs[house_index][2] += min(costs[house_index - 1][0], costs[house_index - 1][1])
# The last row now contains total minimum costs for finishing with each color.
return min(costs[-1])| Case | How to Handle |
|---|---|
| Empty or null costs array | Return zero immediately as no houses need to be painted. |
| Single house scenario where n equals 1 | Return the minimum cost among the three color options for that single house. |
| Large input scale with maximum allowed houses | Iterative dynamic programming ensures linear time complexity and constant auxiliary space without stack overflow. |
| All costs are identical across houses and colors | The transition state logic naturally picks the valid non-adjacent minimums to accumulate the total cost. |
| Extremely skewed cost distributions with very large numbers | Standard 32-bit or 64-bit integers will not overflow if total costs stay within data type limits. |
| Zero or negative cost values in the matrix | Mathematical addition functions correctly with negative values because minimizing sums still holds true. |