zoukankan      html  css  js  c++  java
  • *Paint House II

    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)

    The idea is similar to the problem Paint House I, for each house and each color, the minimum cost of painting the house with that color should be the minimum cost of painting previous houses, and make sure the previous house doesn't paint with the same color.

    We can use min1 and min2 to track the indices of the 1st and 2nd smallest cost till previous house, if the current color's index is same as min1, then we have to go with min2, otherwise we can safely go with min1.

    The code below modifies the value of costs[][] so we don't need extra space.

    public int minCostII(int[][] costs) {
        if (costs == null || costs.length == 0) return 0;
    
        int n = costs.length, k = costs[0].length;
        // min1 is the index of the 1st-smallest cost till previous house
        // min2 is the index of the 2nd-smallest cost till previous house
        int min1 = -1, min2 = -1;
    
        for (int i = 0; i < n; i++) {
            int last1 = min1, last2 = min2;
            min1 = -1; min2 = -1;
    
            for (int j = 0; j < k; j++) {
                if (j != last1) {
                    // current color j is different to last min1
                    costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1];
                } else {
                    costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2];
                }
    
                // find the indices of 1st and 2nd smallest cost of painting current house i
                if (min1 < 0 || costs[i][j] < costs[i][min1]) {
                    min2 = min1; min1 = j;
                } else if (min2 < 0 || costs[i][j] < costs[i][min2]) {
                    min2 = j;
                }
            }
        }
    
        return costs[n - 1][min1];
    }

    reference:https://leetcode.com/discuss/54415/ac-java-solution-without-extra-space

    
    
  • 相关阅读:
    程序自动网站留言,自动登录,自动投票等做法 httpclient 拂晓风起
    CruiseControl 安装 配置 教程 实例 搭建服务器 (CruiseControl + git/svn) 拂晓风起
    编码和字符集的关系 拂晓风起
    PuttyGen生成SSH(key) 带图 TortoiseGit和Github的SSH生成 拂晓风起
    计算时间差,将yyyyMMddHHmmss字符格式转为时间
    C# 操作非标准的xml文件
    SqlServer2000中调度包到作业中,自动执行失败的解决方法
    ReSharper 命名规则
    Js中setInterval、setTimeout不能传递参数问题 及各自的关闭方法
    获取存储过程返回值
  • 原文地址:https://www.cnblogs.com/hygeia/p/5126089.html
Copyright © 2011-2022 走看看