public class Solution { /** * @param costs: n x 3 cost matrix * @return: An integer, the minimum cost to paint all houses */ public int minCost(int[][] costs) { // write your code here int n = costs.length; if(n==0) return 0; int[][] dp = new int[n][3]; dp[0][0]=costs[0][0]; dp[0][1]=costs[0][1]; dp[0][2]=costs[0][2]; for(int i=1;i<n;++i){ for(int j=0;j<3;++j){ dp[i][j]=Integer.MAX_VALUE; for(int k=0;k<3;++k) { if (k != j) { dp[i][j] = Math.min(dp[i][j], dp[i - 1][k]+costs[i][j]); } } } } return Math.min(dp[n-1][0],Math.min(dp[n-1][1],dp[n-1][2])); } }