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 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.
Follow up:
Could you solve it in O(nk) runtime?
链接: http://leetcode.com/problems/paint-house-ii/
题解:
是Paint House I的generalized版本。这回颜色不是RGB三种,而是扩展到了K种。正好可以试试在Paint House I中没用上的想法。思路还是使用DP, 这回我们需要维护一个刷当前房子之前所有房子最小的花费min1,以及倒数第二小的花费min2。然后我们再遍历当前房子i所有color的花费,假如这个颜色与之前i-1号房子的颜色相同,我们选择min2,否则选择min1。比较完所有颜色以后我们记录下来当前的curMin1,curMin2以及current color, 更新min1,min2和lastColor,就可以继续计算下一个房子了。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int minCostII(int[][] costs) {
if(costs == null || costs.length == 0) {
return 0;
}
int min1 = 0, min2 = 0, lastColor = -1; for(int i = 0; i < costs.length; i++) {
int curMin1 = Integer.MAX_VALUE, curMin2 = Integer.MAX_VALUE, curColor = -1;
for(int j = 0; j < costs[0].length; j++) { // loop through all colors
int cost = costs[i][j] + (j == lastColor ? min2 : min1);
if(cost < curMin1) {
curMin2 = curMin1;
curColor = j;
curMin1 = cost;
} else if(cost < curMin2) {
curMin2 = cost;
}
}
min1 = curMin1;
min2 = curMin2;
lastColor = curColor;
} return min1;
}
}
二刷:
方法跟一刷一样。
主要就是保存一个min,一个secondMin,以及刷上次房子所用的颜色lastColor。 在遍历整个数组的过程中,通过比较不断尝试更新min和secondMin,最后返回结果min.
这里要注意的是,在遍历时,当前颜色等于上次刷房颜色时,我们当前的cost是 cost[j] + secondMinCost,即使用不同的两种颜色。而不同颜色的时候,我们直接使用cost[j] + minCost就可以了。
也就是在数组里找到最小和次小两个元素,以及他们的坐标,然后跟之前保存下来的minCost和secondMinCost以及lastColor进行组合判断。
有些操作还是多余,下次希望可以进一步简化。
Java:
public class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int minCost = 0, secondMinCost = 0, lastColor = -1; for (int[] cost : costs) {
int curMin = Integer.MAX_VALUE, curSecondMin = Integer.MAX_VALUE, curColor = -1;
for (int j = 0; j < cost.length; j++) {
int curCost = cost[j] + (j == lastColor ? secondMinCost : minCost);
if (curCost < curMin) {
curSecondMin = curMin;
curMin = curCost;
curColor = j;
} else if (curCost < curSecondMin) {
curSecondMin = curCost;
}
}
minCost = curMin;
secondMinCost = curSecondMin;
lastColor = curColor;
} return minCost;
}
}
Reference:
https://leetcode.com/discuss/71995/easiest-o-1-space-java-solution
https://leetcode.com/discuss/52982/c-dp-time-o-nk-space-o-k
https://leetcode.com/discuss/54415/ac-java-solution-without-extra-space
https://leetcode.com/discuss/54290/accepted-simple-java-o-nk-solution
https://leetcode.com/discuss/60625/fast-dp-java-solution-runtime-o-nk-space-o-1
https://leetcode.com/discuss/68971/5-ms-java-solution-with-o-kn
http://www.cnblogs.com/jcliBlogger/p/4729957.html
https://leetcode.com/discuss/52937/1-line-python-solution-update-to-o-nk
https://www.zhihu.com/question/33113457
265. Paint House II的更多相关文章
- 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:二叉树下的不能相邻,求能 ...
- [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 ...
- [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 ...
- 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 p ...
- [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 ...
- 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 ...
- [LintCode] 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 ...
- [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 ...
- LeetCode Paint House II
原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...
随机推荐
- Querying mergeinfo requires version 3 of the FSFS filesystem schema
环境: jdk 1.7; svn 3.0.4; TortoiseSVN 1.7.13 Subversion 1.7.10; IntelliJ IDEA 13.1.1;win7 64位系统 之前那个 ...
- Material
renderer.material 物理材质 实现二维图上的人物动作 新建Material,选择Shader(著色器)为transparent/diffuse(背景透明),将上图拉到背景图选项中. ...
- android开发 ,对接支付宝,服务器(PHP)校验失败
已备忘记,资料链接: http://my.oschina.net/u/256646/blog/174222 注意: 里面有一个设计到支付宝公钥的地方: 注 意这个是2048位的公钥应该是9行或者10行 ...
- The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit
如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...
- GitHub教程--上传项目四步法 GitBash命令行下使用方法
之前就用过GitHub,感觉用GitHub托管自己的代码非常不错.可是之前用的都是窗口化的TortoiseGit,省了很多命令行的操作,但是个人非常喜欢使用命令行,于是,今天就试着用了用GitBash ...
- Python 文件读和写
- bzoj 1028 暴力枚举判断
昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...
- WinInet:HTTPS 请求出现无效的证书颁发机构的处理
首先,微软提供的WinInet库封装了对网页访问的方法. 最近工作需要从https服务器获取数据,都知道https和http网页的访问方式不同,多了一道证书认证程序,这样就使得https在请求起来比h ...
- Win8必知快捷键汇总
* Win+C:调出应用Charm菜单(开始界面.传统桌面) * Win+D:所有程序最小化,再次按下恢复(开始界面.传统桌面) * Win+E:打开我的电脑(开始界面.传统桌面) * Win+F:调 ...
- 浅谈 OneAPM 在 express 项目中的实践
[编者按]OneAPM 运营团队,近日在 github 上发现了一篇文章,特别奉献给大家.本文作者王宇先生从2015年年初就开始使用我们的产品,也是OneAPM 的忠实用户. OneAPM 是一个优秀 ...