There is a row of n houses, each house can is painted with one of the three colors:red, blue or green. The cost of painting a certain color is different. You had to paint all the houses such, that no, and adjacent houses have the same color.
The cost of painting a certain color was represented by a cost n x 3
matrix. For example, was the cost of costs[0][0]
painting House 0 with color red; costs[1][2]
the cost of Painting House 1 with color green, And so on ... Find the minimum cost-to-paint all houses.
Note:
All costs is positive integers.
Public classSolution { Public intMincost (int[] costs) { if(costs==NULL|| Costs.length==0){ return0; } for(intI=1; i<costs.length; i++) {costs[i][0] + = math.min (costs[i-1][1],costs[i-1][2]); costs[i][1] + = Math.min (costs[i-1][0],costs[i-1][2]); costs[i][2] + = Math.min (costs[i-1][1],costs[i-1][0]); } intn = costs.length-1; returnMath.min (Math.min (costs[n][0], costs[n][1]), costs[n][2]);}}
Reference:https://leetcode.com/discuss/51721/simple-java-dp-solution
*paint House