Skip to content
BytePatterns

Paint Houses Cheaply

EasyDynamic Programming#bottom-up-dp#rolling-variables~20m

Problem

A row of houses must each be painted in one of three colours, and painting a given house a given colour has its own price. Neighbouring houses may not share a colour. Return the cheapest total price for painting the whole row.

Examples

Input:  costs = [[17, 2, 17],
                 [16, 16, 5],
                 [14, 3, 19]]
Output: 10
Why:    paint the houses in the second, third and second colour for 2 + 5 + 3
Input:  costs = [[7, 6, 2]]
Output: 2
Why:    a single house simply takes its cheapest colour
Input:  costs = []
Output: 0
Why:    edge case, an empty row costs nothing

Hints

0 / 3

Stuck on the idea rather than the code? What Is Dynamic Programming? covers it.