3 houses × 3 colours · dp[i][c] = cheapest to paint houses 0..i ending colour c
·
·
·
·
·
·
·
·
·
Grid DP (tabulation)
▸1dp = R × 3 grid2dp[0] = costs[0] // first house, no neighbour3for i in 1..R−1: for c in 0..2:4 dp[i][c] = costs[i][c] + min(dp[i−1][other two colours])56return min(dp[R−1])
state
- costs row 0[17, 2, 17]
- costs row 1[16, 16, 5]
- costs row 2[14, 3, 19]
- statedp[i][c] = cheapest, house i = colour c