zoukankan      html  css  js  c++  java
  • 265. Paint House II 房子涂色K种选择的版本

    [抄题]:

    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, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] 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.

    Example:

    Input: [[1,5,3],[2,9,4]]
    Output: 5
    Explanation: Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5; 
                 Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5. 

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    k个颜色就不知道怎么办了:还是试啊 套一层循环 一个个加

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    三重循环, s 和 j相等的时候就continue掉

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. i j是主变量,所以cost[i][j]都得用, dp[i][j]数组在不变的情况下就是它自己本身

    dp[i][j] = Math.min(dp[i][j], dp[i - 1][s] + costs[i][j]);

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    所以cost[i][j]都得用, dp[i][j]数组在不变的情况下就是它自己本身

    [复杂度]:Time complexity: O(n*k*k) Space complexity: O(n*k)

    [算法思想:迭代/递归/分治/贪心]:

    贪心

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

    class Solution {
        public int minCostII(int[][] costs) {
            //cc
            if (costs == null || costs.length == 0) return 0;
            
            //ini: dp[][], dp[0][k]
            int n = costs.length, k = costs[0].length;
            int[][] dp = new int[n][k];
            for (int j = 0; j < k; j++) {
                dp[0][j] = costs[0][j];
            }
            
            //for loop: continue;
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < k; j++) {
                    dp[i][j] = Integer.MAX_VALUE;
                    for (int s = 0; s < k; s++) {
                        if (s == j) continue;
                        dp[i][j] = Math.min(dp[i][j], dp[i - 1][s] + costs[i][j]);
                    }
                }
            }
            
            //return: compare each costs[i][k]
            int res = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                res = Math.min(res, dp[n - 1][j]);
            }
            
            return res;
        }
    }
    View Code
  • 相关阅读:
    新家开始了
    FreeMarker过时的方法 new Configuration()
    基于Spring封装的Javamail实现邮件发送
    Spring中PropertiesLoaderUtils应用
    SpringBoot基于数据库的定时任务统一管理
    ActiveMQ点对点模式
    springboot整合swagger2
    dubbo简单示例
    SpringCLoud之搭建Zuul网关集群
    微服务项目框架搭建
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9133919.html
Copyright © 2011-2022 走看看