[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 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?
Analysis:
This problem is very elegant if you take the time comlexity constraint into consideration.
It actually share the same dynamic programming idea as Paint House |. If we continue follow the old coding structure, we definitely would end up with the time complexity: O(nk^2).
level 1: n is the total number of houses we have to paint.
level 2: the first k represent for each house we need to try k colors.
level 3: the second k was caused by the process to search the minimum cost (if not use certain color). Apparently, if we want reach the time complexity O(nk), we have to optimize our operation at level 3.
If we choose the color[i][j], how could we reduce the comparision between (color[i-1][0] to color[i-1][k], except color[i-1][j])
And we know there are acutally extra comparisions, since fore each color, we have to find the smallest amongst other colors. There must be way to solve it, Right?
Yup!!! There is a magic skill for it!!!
Let us assume, we have "min_1" and "min_2".
min_1 : the lowest cost at previous stage.
min_2 : the 2nd lowest cost at previous stage. And we have the minimum costs for all colors at previous stage.
color[i-1][k] Then, iff we decide to paint house "i" with color "j", we can compute the minimum cost of other colors at "i-1" stage through following way.
case 1: iff "color[i-1][j] == min_1", it means the min_1 actually records the minimum value of color[i-1][j] (previous color is j), we have to use min_2;
case 2: iff "color[i-1][j] != min_1", it means min_1 is not the value of color[i-1][j] (previous color is not j), we can use the min_1's color.
Note: iff "pre_min_1 == pre_min_2", it means there are two minimum costs, anyway, no matter which color is pre_min_1, we can use pre_min_2.
----------------------------------------------------------
if (dp[j] != pre_min_1 || pre_min_1 == pre_min_2) {
dp[j] = pre_min_1 + costs[i][j];
} else{
dp[j] = pre_min_2 + costs[i][j];
}
----------------------------------------------------------
The way to maintain "min_1" and "min_2".
for (int i = 0; i < len; i++) {
...
min_1 = Integer.MAX_VALUE;
min_2 = Integer.MAX_VALUE;
...
if (dp[j] <= min_1) {
min_2 = min_1;
min_1 = dp[j];
} else if (dp[j] < min_2){
min_2 = dp[j];
}
} Note:
To reduce the burden of handling case, we absolutely could start from i=0, when we could assume all previous cost is 0 since we have no house.
Solution:
public class Solution {
public int minCostII(int[][] costs) {
if (costs == null)
throw new IllegalArgumentException("costs is null");
if (costs.length == 0)
return 0;
int len = costs.length;
int k = costs[0].length;
int min_1 = 0, min_2 = 0;
int pre_min_1, pre_min_2;
int[] dp = new int[k];
for (int i = 0; i < len; i++) {
pre_min_1 = min_1;
pre_min_2 = min_2;
min_1 = Integer.MAX_VALUE;
min_2 = Integer.MAX_VALUE;
for (int j = 0; j < k; j++) {
if (dp[j] != pre_min_1 || pre_min_1 == pre_min_2) {
dp[j] = pre_min_1 + costs[i][j];
} else{
dp[j] = pre_min_2 + costs[i][j];
}
if (dp[j] <= min_1) {
min_2 = min_1;
min_1 = dp[j];
} else if (dp[j] < min_2){
min_2 = dp[j];
}
}
}
return min_1;
}
}
[LeetCode#265] Paint House II的更多相关文章
- [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 ...
- [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 ...
- 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:二叉树下的不能相邻,求能 ...
- 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 ...
- 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 ...
- 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 Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- C# winform线程的使用 制作提醒休息小程序(长时间计算机工作者必备)
最近发现日常的工作中,经常因为敲代码而忘记了休息,晚上眼睛特别的累. 并且经常长时间看着显示器,对眼睛一定是不好的,所以今天开发了一个小程序,用于提醒休息. 下面先看看运行效果: 1.程序启动后,后台 ...
- JAVA 循环在一个数字前面填充0.小例子
输入结果 00000000000567 String bala="567"; 固定长度是14位,怎么循环在bala前面填充00000000000 System.out.printl ...
- 初学HTML5系列三:事件
Window 事件属性 针对 window 对象触发的事件(应用到 <body> 标签): 属性 值 描述 onafterprint script 文档打印之后运行的脚本. onbefor ...
- php编译安装扩展curl
./configure --with-php-config=/opt/software/php5.4/bin/php-configyum install curl curl-devel
- Developing iOS8 Apps with Swift——iOS8概览
iOS 8 概览 斯坦福公开课--Developing iOS8 Apps with Swift学习笔记 想学习Swift,但是相应的教程不是很多,在CoCoaChina社区闲逛时恰好发现了这门课程, ...
- asp.net web api内部培训资料
最近在公司进行了一次asp.net web api的分享吧,不算是培训. 可能大家有些人对Web API的推出目的还不是很了解,可以看看微软官方的定义,其实是为了提供一个好的REST Service方 ...
- AngularJS cookie的使用
Javascript使用document.cookie接口来处理简单的文本cookie,但现在大多数现代浏览器都可以使用html5 API了(sessionstorage和localstorage), ...
- Java程序实现导出Excel,支持IE低版本
来博客园两年多了,最近才开通了微博,因为懒所以也一直没有写东西,今天想整理一下自己前段时间遇到的一个导出的问题. 因为项目的需求,要做一部分导出功能.开始的时候用的公司的导出,但是很奇怪有部分模块导出 ...
- 深入理解JAVA多态原理
之前一直知道多态是什么东西,平时敲代码也经常用到多态,但一直没有真正了解多态底层的运行机制到底是怎么样的,这两天才研究明白点,特地写下来,跟各位同学一起进步,同时也希望各位大神指导和指正. 多态的概念 ...
- [Machine Learning] 梯度下降(BGD)、随机梯度下降(SGD)、Mini-batch Gradient Descent、带Mini-batch的SGD
一.回归函数及目标函数 以均方误差作为目标函数(损失函数),目的是使其值最小化,用于优化上式. 二.优化方式(Gradient Descent) 1.最速梯度下降法 也叫批量梯度下降法Batch Gr ...