[抄题]:

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;
}
}

265. Paint House II 房子涂色K种选择的版本的更多相关文章

  1. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  2. [leetcode]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 paintin ...

  3. [LeetCode] 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 paintin ...

  4. [leetcode]256. Paint House粉刷房子(三色可选)

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  5. 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 pai ...

  6. [LeetCode#265] Paint House II

    Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...

  7. LC 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 paintin ...

  8. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  9. 【BZOJ-1260】涂色paint 区间DP

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1147  Solved: 698[Submit][Sta ...

随机推荐

  1. python3 读入一个jpg格式的图片,并转换长宽像素个数,然后进行绘制

    import matplotlib.pyplot as plt from scipy import ndimage import numpy as np import scipy fname=&quo ...

  2. 20145237 Exp9 Web安全基础实践

    基础问题回答 SQL注入攻击原理,如何防御: 部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,黑客利用这个bug在数据输入区恶意填入脚本,当数据被传回后台,黑客所填入的脚本语句被运行, ...

  3. bzoj 4570: [Scoi2016]妖怪 凸包

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4570 题解 我们知道如果一个怪物要取到攻击力的最大值,那么一定是把防御力都转化了 所以我 ...

  4. intellij idea 清除版本控制

    一.概述 intellij idea 再加入版本控制后,在工作空间中的项目文件都会纳入管理范围,这样idea左侧 "project视图" 中的项目及文件也会出现红色(可能其它颜色) ...

  5. centos 6.5 下 安装 git

    2018-07-02 centos安装Git有两种方式,一种通过源安装,通过如下命今,即可一键安装成功: yum -y install git 但是通过源安装的Git,目前最高版本是1.7,想要使用更 ...

  6. SQL Server中动态列转行

    http://www.cnblogs.com/gaizai/p/3753296.html 一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现 ...

  7. unity的prefab(预设)例子

    prefab用于预先设置一些控件,在需要的时候直接引用,简化开发,当然,你完全可以用写代码解决 在场景内新建一个空物体,绑定一个脚本 void Start () { GameObject cube = ...

  8. "废物利用"也抄袭——“完全”DIY"绘图仪"<二、下位机程序设计>

    就不说怎么组装了吧,一把辛酸泪.说程序,因为这有两把辛酸泪……一把给下位机的C代码一把为了VB.NET的图像处理……不过就上上一篇说的,它们可以正确运行了,并且今天克服了Arduino上电过程中步进电 ...

  9. Python项目输出小类概率,机器学习

    from pandas import read_csv import numpy as np from sklearn.datasets.base import Bunch import pickle ...

  10. [C#]反射相关

    //Type type = DataHelper.Instance.GetType(); //MethodInfo[] infos = type.GetMethods(BindingFlags.Ins ...