资讯专栏INFORMATION COLUMN

[LintCode/LeetCode] Paint House I & Paint Hous

ChristmasBoy / 773人阅读

摘要:在原数组上动规,每一行对应一个房子,每一个元素代表从第一行的房子到这一行的房子选择这一种颜色所花的最小开销。所以每个元素该元素的值上一行两个与该元素不同列元素的值的较小者。不过这次要记录三个变量本行最小值,本行第二小值,本行最小值下标。

Paint House Problem

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 3 cost matrix. For example, costs0 is the cost of painting house 0 with color red; costs1 is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Example

Given costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10

Note

在原数组上动规,每一行对应一个房子,每一个元素代表从第一行的房子到这一行的房子选择这一种颜色所花的最小开销。所以每个元素 = 该元素的值 + 上一行两个与该元素不同列元素的值的较小者
动规到

Solution
public class Solution {
    public int minCost(int[][] cost) {
        if (cost == null || cost.length == 0) return 0;
        int n = cost.length - 1;
        for (int i = 1; i <= n; i++) {
            cost[i][0] += Math.min(cost[i-1][1], cost[i-1][2]);
            cost[i][1] += Math.min(cost[i-1][0], cost[i-1][2]);
            cost[i][2] += Math.min(cost[i-1][0], cost[i-1][1]);
        }
        return Math.min(cost[n][0], Math.min(cost[n][1], cost[n][2]));
    }
}
Paint House II Problem

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.

Example

Given n = 3, k = 3, costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is color 2, house 1 is color 3, house 2 is color 2, 2 + 5 + 3 = 10

Challenge

Could you solve it in O(nk)?

Note

和Paint House的思路一样,依然是在cost数组上更新最小花销之和。不过这次要记录三个变量:本行最小值,本行第二小值,本行最小值下标。这三个变量每行更新一次,同时也要存储上一次循环的三个变量。这样做的目的是当前一行最小值和之前一行最小值在同一列时,取之前一行的次小值,也就避免了相邻两行取同种颜色的问题。

Solution
public class Solution {
    public int minCostII(int[][] cost) {
        if (cost == null || cost.length == 0) return 0;
        int preMin = 0, preSec = 0, preIndex = -1;
        int n = cost.length, k = cost[0].length;
        for (int i = 0; i < n; i++) {
            int curMin = Integer.MAX_VALUE, curSec = Integer.MAX_VALUE, curIndex = 0;
            for (int j = 0; j < k; j++) {
                cost[i][j] += preIndex == j ? preSec : preMin;
                if (cost[i][j] < curMin) {
                    curSec = curMin;
                    curMin = cost[i][j];
                    curIndex = j;
                }
                else if (cost[i][j] < curSec) {
                    curSec = cost[i][j];
                }
            }
            preMin = curMin;
            preSec = curSec;
            preIndex = curIndex;
        }
        return preMin;
    }
}

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

转载请注明本文地址:https://www.ucloud.cn/yun/66190.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
  • 265. 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 w...

    mylxsw 评论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
  • 276. Paint Fence

    摘要:代码如下表示跟前面不一样颜色,表示跟前面一样颜色跟前面不一样颜色的话,在这轮有种可能性跟前面一样颜色的话,在这轮有种可能性,且前一轮不能与前前一轮一样颜色因为这个的解法里,我们只用到变量和,所以我们可以进定步把空间复杂度降为 题目:There is a fence with n posts, each post can be painted with one of the k colo...

    zhjx922 评论0 收藏0

发表评论

0条评论

ChristmasBoy

|高级讲师

TA的文章

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