A housing society repaints a street of houses. Each house can be painted in one of three colours, numbered 0, 1 and 2, and the quote for each house and colour is different. Neighbouring houses must not share a colour.
costs is an array with one entry per house, in street order. Each entry is an array of three non-negative integers: the cost of painting that house in colour 0, 1 and 2.
Write two functions:
minPaintingCost(costs) returns the lowest total cost to paint every house so that no two adjacent houses have the same colour. An empty street costs 0.
cheapestPaintPlan(costs) returns one cheapest plan as an array of colour numbers, one per house. If several plans have the same lowest cost, return the lexicographically smallest one: the plan whose first house has the smallest colour number, then the second house, and so on. An empty street returns [].
Rules:
Only adjacent houses are restricted: houses two apart may share a colour.
If costs is not an array of three-element arrays of non-negative safe integers, throw a TypeError.
A street can have 100,000 houses. Trying every colouring is exponential, and a recursive solution runs out of call stack, so solve it iteratively in O(n).