码迷,mamicode.com
首页 > 其他好文 > 详细

*Paint House II

时间:2016-01-13 02:03:22      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Follow up:
Could you solve it in O(nk) runtime?

 

解法一:O(nkk) ...

public class Solution {
    public int minCostII(int[][] costs) {
    if(costs==null||costs.length==0){
        return 0;
    }
    
    for(int i=1; i<costs.length; i++)
    {
        for(int j=0; j<costs[0].length;j++)
        {
            int min = Integer.MAX_VALUE;
            for(int k=0; k<costs[0].length;k++)
            {
                if(k==j) continue;
                min = Math.min(min,costs[i-1][k]);
            }
            costs[i][j] += min;
        }
    }
    
    int n = costs.length-1;
    int min = Integer.MAX_VALUE;
    for(int j=0; j<costs[0].length;j++)
    {
        min = Math.min(min,costs[n][j]);
    }
    return min;
    }
}

 

 

解法二:O(nk)

Explanation: dp[i][j] represents the min paint cost from house 0 to house i when house i use color j; The formula will be dp[i][j] = Math.min(any k!= j| dp[i-1][k]) + costs[i][j].

Take a closer look at the formula, we don‘t need an array to represent dp[i][j], we only need to know the min cost to the previous house of any color and if the color j is used on previous house to get prev min cost, use the second min cost that are not using color j on the previous house. So I have three variable to record: prevMin, prevMinColor, prevSecondMin. and the above formula will be translated into: dp[currentHouse][currentColor] = (currentColor == prevMinColor? prevSecondMin: prevMin) + costs[currentHouse][currentColor].

public class Solution {
public int minCostII(int[][] costs) {
    if(costs == null || costs.length == 0 || costs[0].length == 0) return 0;

    int n = costs.length, k = costs[0].length;
    if(k == 1) return (n==1? costs[0][0] : -1);

    int prevMin = 0, prevMinInd = -1, prevSecMin = 0;//prevSecMin always >= prevMin
    for(int i = 0; i<n; i++) {
        int min = Integer.MAX_VALUE, minInd = -1, secMin = Integer.MAX_VALUE;
        for(int j = 0; j<k;  j++) {
            int val = costs[i][j] + (j == prevMinInd? prevSecMin : prevMin);
            if(minInd< 0) {min = val; minInd = j;}//when min isn‘t initialized
            else if(val < min) {//when val < min, 
                secMin = min;
                min = val;
                minInd = j;
            } else if(val < secMin) { //when min<=val< secMin
                secMin = val;
            }
        }
        prevMin = min;
        prevMinInd = minInd;
        prevSecMin = secMin;
    }
    return prevMin;
}
}

reference:https://leetcode.com/discuss/60625/fast-dp-java-solution-runtime-o-nk-space-o-1

*Paint House II

标签:

原文地址:http://www.cnblogs.com/hygeia/p/5126089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!