Show problems as
Classic problem #256MediumArrayDynamic Programming

Paint House

Real-world version: Finishing a Row of Panels
Tracked in this browser only
Read Paint House on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A row of panels must each be given one of three finishes. prices[i][j] is what it costs to put finish j on panel i. Two panels standing next to each other may not share a finish.

Return the least it can cost to finish the whole row.

Examples

Example 1

Input
prices = [[15, 3, 15], [14, 14, 4], [12, 2, 17]]
Output
9

Give the first panel the second finish for 3, the middle panel the third finish for 4 and the last panel the second finish for 2. No two neighbours match and the bill comes to 9.

Example 2

Input
prices = [[8, 6, 2]]
Output
2

One panel has no neighbour, so the cheapest of its three finishes wins.

Example 3

Input
prices = [[1, 2, 3], [1, 20, 20]]
Output
3

Taking the cheapest finish on the first panel would push the second panel onto a finish costing 20. Paying 2 on the first panel lets the second cost 1.

Constraints

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

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def min_cost(prices: list[list[int]]) -> int:
Java
public int minCost(int[][] prices)