Mid/Senior

Paint House

There is a row of n houses, and each house can be painted one of three colors: red, blue, or green. The cost of painting each house with each color is given by a 2D integer array costs, where costs[i][0], costs[i][1], and costs[i][2] are the costs of painting house i red, blue, and green, respectively.

You must paint all houses such that no two adjacent houses have the same color.

Return the minimum total cost to paint all houses.

Example 1
Inputcosts = [[17,2,17],[16,16,5],[14,3,19]]
Output10
Paint house 0 blue, house 1 green, and house 2 blue for a total cost of 2 + 5 + 3 = 10.
Example 2
Inputcosts = [[7,6,2]]
Output2
With only one house, choose the cheapest color, which costs 2.

Constraints

  • costs.length == n
  • costs[i].length == 3
  • 1 <= n <= 100
  • 1 <= costs[i][j] <= 20

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate