/** 256.Paint House * 2016-6-21 by Mingyang * Typical DP, I thought from the beginning that if you use another value of a two-dimensional dp,dp to represent the color * The most advanced part of this place is! I was updating the cost of the matrix, and did not specifically come up with a DP array very clever! */ Public intMincost (int[] costs) { //Please remember the simplest case. if(Costs! =NULL&& Costs.length = = 0) return0; for(inti = 1; i < costs.length; i++){ //In the first color, the Last house cannot be painted in the first color, so we have to find the smallest of the second and third colors in the Last house plusCosts[i][0] = costs[i][0] + math.min (costs[i-1][1], costs[i-1][2]); //Apply the second or third color similarlyCOSTS[I][1] = costs[i][1] + math.min (costs[i-1][0], costs[i-1][2]); costs[i][2] = costs[i][2] + math.min (costs[i-1][0], costs[i-1][1]); } //returns the least expensive of the three colors. returnMath.min (Costs[costs.length-1][0], math.min (costs[costs.length-1][1], costs[costs.length-1][2])); }
256.Paint House