Design a DP: paint the houses
Challenge · Write code · about 20 min
A row of houses must each be painted red, green or blue, and no two neighbours may share a colour. costs[i][k] is the price of painting house i with colour k.
Return the cheapest total. Design the DP yourself: choose a state, write the recurrence, then code it.
Example 1
Input:
[[[17,2,17],[16,16,5],[14,3,19]]]
Output:
10
Green, blue, green: 2 + 5 + 3.
Example 2
Input:
[[[7,6,2]]]
Output:
2
Constraints
- 0 ≤ costs.length ≤ 1,000
- costs[i].length = 3
- 1 ≤ costs[i][k] ≤ 20
Learn the idea first: Recognizing DP and choosing the state, a visual lesson.
More Recognizing DP and choosing the state problems
- Pick the state Warm-up
- Write the recurrence Standard
- Courier tolls Standard
- Domino tilings Standard
- Walk the flowchart Standard