Paint House II(房屋染色 II)

http://www.lintcode.com/en/problem/paint-house-ii/
请参阅 Paint House(房屋染色)

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

你可能感兴趣的:(Paint House II(房屋染色 II))