资讯专栏INFORMATION COLUMN

265. Paint House II

mylxsw / 3384人阅读

摘要:题目解答利用的思路,只不过把三种颜色换成了种颜色,所以是如何把它的复杂度降到那么就是如果将颜色的部分只扫一遍。参考的里只需要记录下每个的最小的两个颜色。

题目:
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, costs0 is the cost of painting house 0 with color 0; costs1 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?

解答:
利用paint house的思路,只不过把三种颜色换成了k种颜色,所以:

//State: f[i][j] is the minimum cost of painting i houses with color j
    //Function: f[i][j] = Math.min(f[i - 1][k(except j)]) + costs[i][j];
    //Initialize: f[0][j] = costs[0][j];
    //Result: Math.min(f[costs.length - 1][j]);
    public int minCostII(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length == 0) {
            return 0;
        }
        int house = costs.length, color = costs[0].length;
        int[][] f = new int[house][color];
        for (int i = 0; i < color; i++) {
            f[0][i] = costs[0][i];
        }
        
        for (int i = 1; i < house; i++) {
            for (int j = 0; j < color; j++) {
                f[i][j] = Integer.MAX_VALUE;
                for (int k = 0; k < color; k++) {
                    if (k == j) continue;
                    f[i][j] = Math.min(f[i][j], f[i - 1][k] + costs[i][j]);
                }
            }
        }
        
        int result = Integer.MAX_VALUE;
        for (int i = 0; i < color; i++) {
            result = Math.min(result, f[house - 1][i]);
        }
        
        return result;

followup是如何把它的复杂度降到o(nk), 那么就是如果将颜色的部分只扫一遍。参考leetcode的discuss里most voted answer, 只需要记录下每个house的最小的两个颜色。如果下一个颜色跟这个颜色不一样,就取最小的这个颜色加上这次所选的颜色,并找出最小值;如果下一个颜色跟这个颜色一样,那么我们不可以取最小的这个颜色,所以我们取第二小的颜色加上这次所选的颜色。最后把最小的颜色输出就可以了。

//Only the first two minimum costs count, so we keep track on min1 and min2 for each house
    public int minCostII(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length == 0) {
            return 0;
        }
        
        int house = costs.length, color = costs[0].length;
        
        int min1 = -1, min2 = -1;
        for (int i = 0; i < house; i++) {
            int last1 = min1, last2 = min2;
            min1 = -1; min2 = -1;
            
            for (int j = 0; j < color; j++) {
                if (j != last1) {
                    costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1];
                } else {
                    costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2];
                }
                
                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[house - 1][min1];
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64829.html

相关文章

  • [Leetcode] Paint House 房子涂色

    摘要:动态规划复杂度时间空间思路直到房子,其最小的涂色开销是直到房子的最小涂色开销,加上房子本身的涂色开销。我们在原数组上修改,可以做到不用空间。代码找出最小和次小的,最小的要记录下标,方便下一轮判断 Paint House There are a row of n houses, each house can be painted with one of the three colors...

    SunZhaopeng 评论0 收藏0
  • [LintCode/LeetCode] Paint House I & Paint Hous

    摘要:在原数组上动规,每一行对应一个房子,每一个元素代表从第一行的房子到这一行的房子选择这一种颜色所花的最小开销。所以每个元素该元素的值上一行两个与该元素不同列元素的值的较小者。不过这次要记录三个变量本行最小值,本行第二小值,本行最小值下标。 Paint House Problem There are a row of n houses, each house can be painted ...

    ChristmasBoy 评论0 收藏0
  • 256. Paint House

    摘要:题目解答这类题还是先找临时的结果,由临时的结果最终推出最终解。比如说用存到个的时候最小的但是到第个的时候,有三种情况涂当我涂红的时候,前面一个只能涂蓝或者绿,所以我只能加上这两种情况的最小值,作为此次计算的最小值,以此类推。 题目:here are a row of n houses, each house can be painted with one of the three co...

    Miracle 评论0 收藏0
  • [LintCode/LeetCode] House Robber II

    摘要:因为取了队首就不能取队尾,所以对数组循环两次,一个从取到,一个从取到,比较两次循环后队尾元素,取较大者。注意,要先讨论当原数组位数小于时的情况。 Problem After robbing those houses on that street, the thief has found himself a new place for his thievery so that he wi...

    OnlyLing 评论0 收藏0
  • [LeetCode] House Robber I II

    摘要:注意对边界条件的判断,是否非空,是否长度为 House Robber I Problem You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...

    qpal 评论0 收藏0

发表评论

0条评论

mylxsw

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<