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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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:二叉树下的不能相邻,求能 ...

  4. 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 ...

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

  6. 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 ...

  7. [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 ...

  8. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  9. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. ECshop--搜索模块细究

    ecshop细究 今天看了下ecshop搜索这块,前台数据一直到后台查询再返回前台.大致是这么个过程,首先,在index.dwt文件内,body下面引入了 <!-- #BeginLibraryI ...

  2. CentOS&nbsp;6.4&nbsp;图文安装教…

    点评:CentOS 6.4是最新的出的系统,这里分享下安装教程,有些设置大部分教程没出现过,特分享下,方便需要的朋友 CentOS 6.4 下载地址: http://www.jb51.net/soft ...

  3. less编码规范

    Less 编码规范 简介 因为自己最近写css用的比较多还是less,整理了一份less规范, 代码组织 代码按如下形式按顺序组织: @import 变量声明 样式声明 // ✓ @import &q ...

  4. Http,Https(SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2的配置和使用 分类: ASP.NET 2014-11-05 12:51 97人阅读 评论(0) 收藏

    下载地址1:https://securityswitch.googlecode.com/files/SecuritySwitch%20v4.2.0.0%20-%20Binary.zip 下载地址2:h ...

  5. jmeter压测SSL加密网站

    1.生成秘钥文件 得到网站证书,用jdk自带的keytool生成秘钥文件,执行dos命令: keytool     -import -alias "aaa" -file " ...

  6. Linux上安装KDE, Gnome和VNC

    安装所需环境:需要至少256m的可用内存(128也可以不过有点卡)CentOS或类似OS(Debian的话改成apt-get应该也可以) 1,安装KDE 1 yum install kdepim 或安 ...

  7. [HttpException (0x80004005): Failed to Execute URL.]之画蛇添足之痛

    最近很悲惨,发布的一个mvc站点,所有的静态内容,如js.css.图片都不能正常加载,服务器给出的响应是一个如下的异常黄页: Server Error in '/ua' Application.Fai ...

  8. Object-C 设计类接口

    在Object-C中,一个类通常分为两部分,.h头文件和.m实现文件. 类的接口(interface)通常存放在类似ClassName.h的文件中.在这里我们定义实例变量和公用(public)方法. ...

  9. 查看Safari和钥匙串中的密码

    Safari Safari的同步书签功能很棒,还可以看到其他设备没关掉的网页.为了省时间,一些经常进的网站,比如博客,邮箱等,我都会选择让Safari保存密码,还使用iCloud同步!因为一直很放心苹 ...

  10. CSS 导航栏

    实例: 导航栏 Home News Articles Forum Contact About 导航栏 熟练使用导航栏,对于任何网站都非常重要. 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜 ...