zoukankan      html  css  js  c++  java
  • 515. 房屋染色(序列型动态规划)

    515. 房屋染色

    中文English

    这里有n个房子在一列直线上,现在我们需要给房屋染色,分别有红色蓝色和绿色。每个房屋染不同的颜色费用也不同,你需要设计一种染色方案使得相邻的房屋颜色不同,并且费用最小,返回最小的费用。

    费用通过一个nx3 的矩阵给出,比如cost[0][0]表示房屋0染红色的费用,cost[1][2]表示房屋1染绿色的费用。

    样例

    样例 1:

    输入: [[14,2,11],[11,14,5],[14,3,10]]
    输出: 10
    解释: 第一个屋子染蓝色,第二个染绿色,第三个染蓝色,最小花费:2 + 5 + 3 = 10.
    

    样例 2:

    输入: [[1,2,3],[1,4,6]]
    输出: 3
    

    注意事项

    所有费用都是正整数

     
    输入测试数据 (每行一个参数)如何理解测试数据?
    class Solution:
        """
        @param costs: n x 3 cost matrix
        @return: An integer, the minimum cost to paint all houses
        """
        """
        大致思路:
        1.确定状态
        l = len(costs)
        最后一步:dp[l - 1]
        子问题:
        转化为前n - 1栋房子的最小花费,然后实际会求得3个花费值出来,分别是dp[i - 1][0],dp[i - 1][1],dp[i - 1][2]
    
        2.转移方程,下面也不会出现颜色相等的情况了,上一个的费用 + 当前颜色的费用
        dp[i][0] = min(dp[i - 1][1] + costs[1],dp[i - 1][2] + costs[2])
        dp[i][1] = min(dp[i - 1][0] + costs[0],dp[i - 1][2] + costs[2])
        dp[i][2] = min(dp[i - 1][0] + costs[0],dp[i - 1][1] + costs[1])
    
        3.初始条件
        dp = [sys.maxsize]*l
    
        """
        def minCost(self, costs):
            # write your code here
            if not costs:return 0
            
            #初始化
            l = len(costs)
            dp = [[sys.maxsize]*3 for _ in range(l)]
    
            #计算顺序
            for i in range(l):
                if (i == 0):
                    dp[0][0] = costs[0][0]
                    dp[0][1] = costs[0][1]
                    dp[0][2] = costs[0][2]
                    continue
                
                #当前为红,取上一个为蓝的最小总费用 + 当前红的费用 和上一个为绿的最小总费用 + 当前为红的费用,取出最小值出来
                dp[i][0] = min(dp[i - 1][1] + costs[i][0],dp[i - 1][2] + costs[i][0])
                dp[i][1] = min(dp[i - 1][0] + costs[i][1],dp[i - 1][2] + costs[i][1])
                dp[i][2] = min(dp[i - 1][0] + costs[i][2],dp[i - 1][1] + costs[i][2])
            
            return min(dp[l - 1])

    优化版:(不确定多少种染料)

    class Solution:
        """
        @param costs: n x 3 cost matrix
        @return: An integer, the minimum cost to paint all houses
        """
        def minCost(self, costs):
            # write your code here
            if not costs:return 0
            
            #初始化
            l = len(costs)
            c = len(costs[0])
            dp = [[sys.maxsize]*c for _ in range(l)]
            
            #计算顺序
            for i in range(l):
                for j in range(c):
                    #如果是0的话,说明是第一个房子,则直接分别得到染三种颜色的不同花费多少
                    if (i == 0):
                        dp[0][j] = costs[0][j]
                        #不用continue,i = 0 的有c种情况
        
                    #循环取出当前房子的颜色j,和上一个房子的颜色为k,的所有总花费出来
                    for k in range(c):
                        if (k != j):
                        #分别是dp[i][0],dp[i][1],dp[i][2]...求得的,不同的最小值花费和
                            dp[i][j] = min(dp[i - 1][k] + costs[i][j],dp[i][j])
            
            return min(dp[l - 1])
                            
                        
                     
  • 相关阅读:
    ABAP接口用法
    监听textarea数值变化
    The first step in solving any problem is recognizing there is one.
    Wrinkles should merely indicate where smiles have been.
    God made relatives.Thank God we can choose our friends.
    Home is where your heart is
    ABAP跳转屏幕
    Python 工具包 werkzeug 初探
    atom通过remote ftp同步本地文件到远程主机的方法
    Mongodb学习笔记一
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/13028896.html
Copyright © 2011-2022 走看看