All problems
1100MediumArrayDynamic Programming

Finishing a Row of Panels

Tracked in this browser only
Write code

Trains the technique from

LeetCode 256Paint House

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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