Leetcode 265. Paint House II

这篇博客介绍了三种使用动态规划方法解决最低成本涂色问题的算法,分别是:基于原数组修改的DP方法,1D dp不改变原数组的方法,以及空间复杂度为1的DP方法。每种方法的时间复杂度均为n*k^2,其中n为房子数量,k为颜色数量。博客提供了详细的代码实现和复杂度分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
方法1: 和道题和256题基本一模一样,只是颜色的数量不是固定的了。所以256的方法基本都是用于这道题目。方法1是dp,时间复杂n*k^2,n为房子数,k为颜色数,时间复杂1。这个做法会改变原数组。

class Solution {
    public int minCostII(int[][] costs) {
        if(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] = costs[i][j] + min;
            }
        }
        int res = Integer.MAX_VALUE;
        for(int i = 0; i < costs[0].length; i++){
            res = Math.min(res, costs[costs.length - 1][i]);
        }
        return res;
    }
}

方法2: 1D dp。时间复杂n*k^2,空间复杂k。这个方法不会改变原数组,这个方法也是保留原数组的最好的算法。详细解释直接看lc官方解答3.

class Solution {
    public int minCostII(int[][] costs) {
        if(costs.length == 0) return 0;
        int k = costs[0].length;
        int n = costs.length;
        int[] prev = costs[0];
        
        for(int i = 1; i < n; i++){
            int[] curr = new int[k];
            for(int j = 0; j < k; j++){
                int min = Integer.MAX_VALUE;
                for(int m = 0; m < k; m++){
                    if(m == j) continue;
                    min = Math.min(min, prev[m]);
                }
                curr[j] = costs[i][j] + min;
            }
            prev = curr;
        }
        
        int res = Integer.MAX_VALUE;
        for(int num : prev){
            res = Math.min(res, num);
        }
        return res;
    }
}

方法3: dp,modify original array,时间复杂nk,空间复杂1。也很简单,详细解释直接看lc官方解答4.

class Solution {
    public int minCostII(int[][] costs) {
        if(costs.length == 0) return 0;
        int k = costs[0].length;
        int n = costs.length;
        
        for(int i = 1; i < n; i++){
            int min = -1; int sec = -1;
            for(int j = 0; j < k; j++){
                int cost = costs[i - 1][j];
                if(min == -1 || cost < costs[i - 1][min]){
                    sec = min;
                    min = j;
                }else if(sec == -1 || cost < costs[i - 1][sec]){
                    sec = j;
                }
            }
            
            for(int j = 0; j < k; j++){
                if(j == min){
                    costs[i][j] += costs[i - 1][sec];
                }else{
                    costs[i][j] += costs[i - 1][min];
                }
            }
        }
        
        int res = Integer.MAX_VALUE;
        for(int num : costs[n - 1]){
            res = Math.min(res, num);
        }
        return res;
    }
}

总结:

### LeetCode Problem 256 Paint House Python 解决方案 对于LeetCode上的第256题“Paint House”,该问题涉及动态规划算法来最小化成本。给定一个`n x k`大小的成本矩阵,其中每一行代表一所房子,每一列代表一种颜色的成本。目标是以最低总成本粉刷所有房屋,条件是相邻两所房子的颜色不同。 #### 动态规划方法概述 为了有效地解决这个问题,采用自底向上的动态规划策略。创建一个新的二维数组`dp`用于存储到达每栋房子时选择特定颜色所需的累积最小花费。初始化阶段设置第一座房子的各项费用等于输入数据中的对应值[^1]。 迭代过程中更新剩余各层的状态转移方程如下所示: ```python for house in range(1, n): for color in range(k): dp[house][color] = cost[house][color] + min(dp[house-1][:color] + dp[house-1][color+1:]) ``` 最终结果即为最后一排中最小的那个数,表示完成整个项目所需支付的最少金额。 #### 完整代码实现 下面提供了一个完整的Python函数来计算最优解决方案: ```python def minCostII(costs: list[list[int]]) -> int: if not costs or not costs[0]: return 0 n = len(costs) # number of houses k = len(costs[0]) # number of colors # Initialize DP table with infinity values except first row which copies from costs. dp = [[float('inf')]*k for _ in range(n)] dp[0] = costs[0] for house in range(1, n): for color in range(k): previous_row_except_current_color = dp[house-1][:color]+dp[house-1][color+1:] dp[house][color] = costs[house][color] + min(previous_row_except_current_color) return min(dp[-1]) ``` 此版本的时间复杂度接近O(n*k^2),因为每次都需要遍历前一排除当前索引外的所有元素寻找最小值。如果考虑进一步优化性能,则可以通过维护两个变量分别记录上一轮次最小和第二小的成本及其对应的油漆编号,在此基础上调整状态转换逻辑从而降低时间消耗至线性级别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值