[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#中的static静态变量的用法
静态全局变量 定义:在全局变量前,加上关键字 static 该变量就被定义成为了一个静态全局变量. 特点: A.该变量在全局数据区分配内存. B.初始化:如果不显式初始化,那么将被隐式初始化为0. 静 ...
- css height:100%失效
有时做移动端页面时,需要用到height:100%来控制,但是设置完后会发现,用百分比的高是不生效的. 经过上网浏览等查阅资料得知,是否可以使用百分比是根据父级对象定义的.所以解决方法就是在body和 ...
- JS0热身运动
热身热身小知识点: JS中如何获取元素: 1 通过ID名称来获取:document get element by id -->document.getElementById() 2 .... ...
- not exists 跟not in 纪念一下
- 分享:根据svg节点对象类型和路径值转换坐标值
功能用处: 对svg文件的路径节点填充时会使用(相邻两个坐标区域内的四边形的填充颜色不重复). 需要对svg文件中的Path节点或者 Polyline 节点做颜色填充.并且相邻的两个区域之间的颜色不允 ...
- 懒人神器之T4模板
最近遇到一个比较令人烦躁的问题,特别是对于我等懒癌末期者.实在难以忍受!具体问题是这样,这个项目是一个新的项目.使用EF框架来开发,那么在搭建架构时,当我们新加一个Entity时,就需要在每个层级添加 ...
- 如何使用event 10049分析定位library cache lock and library cache pin
Oracle Library Cache 的 lock 与 pin 说明 一. 相关的基本概念 之前整理了一篇blog,讲了Library Cache 的机制,参考: Oracle Library c ...
- 禁用UITextField复制粘贴等方法
要实现此功能只需创建一个继承自UITextField的子类,重写以下方法即可. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ ...
- 设置透明navigationBar
三行代码轻松实现透明navigationBar: [self.navigationController.navigationBar setBackgroundImage:[UIImage new] ...
- 使用sprintf打印float并控制小数位数时引起的问题
最近在做项目中发现一个Bug,直接把进程搞死,查了一下,居然是一个最不起眼的地方导致的,在此记录一下. 先看下面代码 #include <iostream> #include <st ...