Paint House
Real-world version: Finishing a Row of PanelsThe 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.
def min_cost(prices: list[list[int]]) -> int:public int minCost(int[][] prices)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's