/** 265. Paint House II * 2016-6-24 by Mingyang * This was a classic back pack problem. *--Define dp[n][k], where dp[i][j] means for house I with a color J the minimum cost. *--Initial value:dp[0][j] = costs[0][j]. For others, dp[i][j] = Integer.max_value;, I >= 1 *--Transit function:dp[i][j] = Math.min (Dp[i][j], dp[i-1][ K] + cost[i][j]), where k! = J. *--Final state:min (Dp[n-1][k]). */ Public intMincostii (int[] costs) { if(Costs = =NULL|| Costs.length = = 0) { return0; } intn =costs.length; intK = Costs[0].length; //dp[i][j] means the min cost painting for house I, with Color J int[] DP =New int[N][k]; //initialization for(inti = 0; I < K; i++) {dp[0][i] = costs[0][i]; } for(inti = 1; I < n; i++) { for(intj = 0; J < K; J + +) {Dp[i][j]=Integer.max_value; for(intm = 0; M < K; m++) { if(M! =j) {Dp[i][j]= Math.min (Dp[i-1][m] +Costs[i][j], dp[i][j]); } } } } //Final State intMincost =Integer.max_value; for(inti = 0; I < K; i++) {Mincost= Math.min (Mincost, dp[n-1][i]); } returnMincost; }
265. Paint House II