资讯专栏INFORMATION COLUMN

256. Paint House

Miracle / 462人阅读

摘要:题目解答这类题还是先找临时的结果,由临时的结果最终推出最终解。比如说用存到个的时候最小的但是到第个的时候,有三种情况涂当我涂红的时候,前面一个只能涂蓝或者绿,所以我只能加上这两种情况的最小值,作为此次计算的最小值,以此类推。

题目:
here 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.

Note:
All costs are positive integers.

解答:
这类题还是先找临时的结果,由临时的结果最终推出最终解。比如说用f(i, j)存到i个house的时候最小的cost.但是到第i个house的时候,有三种情况:涂red, blue or green.当我涂红的时候,前面一个只能涂蓝或者绿,所以我只能加上这两种情况的最小值,作为此次计算的最小值,以此类推。

public class Solution {
    //State: f[i][j] is the minimum cost of painting i houses with color j -> (red, blue, green)
    //Function: f[i][0] = Math.min(f[i - 1][1], f[i - 1][2]) + costs[i][0]
    //          f[i][1] = Math.min(f[i - 1][0], f[i - 1][2]) + costs[i][1]
    //          f[i][2] = Math.min(f[i - 1][0], f[i - 1][1]) + costs[i][2]
    //Initialize: f[0][0] = costs[0][0], f[0][1] = costs[0][1], f[0][2] = costs[0][2];
    //Result: Math.min(f[costs.length - 1][0], f[costs.length - 1][1], f[costs.length - 1][2]);
    public int minCost(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];
        //Initialize
        f[0][0] = costs[0][0];
        f[0][1] = costs[0][1];
        f[0][2] = costs[0][2];
        //Function
        for (int i = 1; i < house; i++) {
            f[i][0] = Math.min(f[i - 1][1], f[i - 1][2]) + costs[i][0];
            f[i][1] = Math.min(f[i - 1][0], f[i - 1][2]) + costs[i][1];
            f[i][2] = Math.min(f[i - 1][0], f[i - 1][1]) + costs[i][2];
        }
        
        return Math.min(f[house - 1][0], Math.min(f[house - 1][1], f[house - 1][2]));
    }
}

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

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

相关文章

  • [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
  • [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
  • 276. Paint Fence

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

    zhjx922 评论0 收藏0
  • Redis新手指南:在node中使用redis

    摘要:简介是一个高性能的数据库,把数据存在内存中,并在磁盘中记录数据的变化。因为将数据存在内存中,所以数据操作非常快。在中使用首先,安装驱动支持多种数据类型,常用的有键值对,哈希表,链表,集合等。键值对运行,结果如下哈希表哈希表有点类似中的。 Redis简介 Redis是一个高性能的key-value数据库,Redis把数据存在内存中,并在磁盘中记录数据的变化。因为将数据存在内存中,所以数据...

    pcChao 评论0 收藏0

发表评论

0条评论

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