Question

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?

Solution

这里时间复杂度是O(nk),说明要求我们用O(k)的时间计算每一层的新的cost。

cost'[i] = costs[m][i] + min{cost[0], cost[1], ..., cost[i - 1], cost[i + 1], ..., cost[k - 1]}

原想法是对每一个i重新计算,时间复杂度是O(k2)。包含了大量的重复计算

其实我们只需求出cost[]序列的最小值和第二小的值。Time complexity O(k)

 public class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length < 1) {
return 0;
}
int m = costs.length, k = costs[0].length;
int[] cost = new int[k];
int[] tmp = new int[k];
for (int i = 0; i < k; i++) {
cost[i] = costs[m - 1][i];
}
for (int i = m - 2; i >= 0; i--) {
// calculate most and second minimum number
int[] min = calcMin(cost);
for (int j = 0; j < k; j++) {
// if cost[j] is minimum, then add second minimum with costs[i][j]
if (cost[j] == min[0]) {
cost[j] = min[1] + costs[i][j];
} else {
// if cost[j] is not minimum, then add minimum with costs[i][j]
cost[j] = min[0] + costs[i][j];
}
}
}
if (k < 2) {
return cost[0];
}
int[] result = calcMin(cost);
return result[0];
} private int[] calcMin(int[] nums) {
if (nums == null || nums.length < 2) {
return new int[0];
}
int[] mins = new int[2];
mins[0] = Math.min(nums[0], nums[1]);
mins[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
if (nums[i] < mins[0]) {
mins[1] = mins[0];
mins[0] = nums[i];
} else if (nums[i] < mins[1]) {
mins[1] = nums[i];
}
}
return mins;
}
}

Paint House II 解答的更多相关文章

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

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

  3. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

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

  5. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

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

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

  8. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  9. [Swift]LeetCode265.粉刷房子 II $ 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 ...

随机推荐

  1. 最牛逼的的shell命令

    参考 远程diff [root@jiangyi01.sqa.zmf /home/ahao.mah/ALIOS_QA/tools/iperf] #ssh ahao.mah@dnstest02.tbc c ...

  2. Gradle[0]依赖本地JAR和远程仓库JAR的配置

    1.对本地Jar的依赖配置 如果不知道Jar包的远程仓库地址,而项目中又要使用该Jar包,就需要进行本地设置. 例如,需要使用的Jar包为sigar.jar,则需要在项目根目录下建目录:libs,并把 ...

  3. IOS XMPP

    http://www.cnblogs.com/lmyhao/p/4120616.html

  4. (转) xcodebuild和xcrun自动化编译ipa包 笔记

    转自:http://blog.csdn.net/totogo2010/article/details/8883100 打包过程 xcodebuild负责将工程源文件编译成xxx.app xcrun负责 ...

  5. Bestcoder #47 B Senior&#39;s Gun

    Senior's Gun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  6. iOS开发之多媒体API

    播放视频 视频文件介绍 视频格式可以分为适合本地播放的本地影像视频和适合在网络中播放的网络流媒体影像视频两大类.尽管后者在播放的稳定性和播放画面质量上可能没有前者 优秀,但网络流媒体影像视频的广泛传播 ...

  7. (第三章)Java内存模型(下)

    一.happens-before happens-before是JMM最核心的概念.对于Java程序员来说,理解happens-before是理解JMM的关键. 1.1 JMM的设计 从JMM设计者的 ...

  8. 网站项目后台的目录命名为admin后,网页莫名其妙的变样了

    这是我的第一篇博客文章,与其说是分享经验,倒不如说是求助 最近因为要完成一个课程设计,在拿一个现成的项目过来改,要用到select下拉菜单,可是发觉怎么我的这个下拉菜单怎么变样了 刚开始它是这样的 感 ...

  9. IIS发布程序,出现:请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理解决方案

    windows 7(或者windows 2008)+iis7.5 出现如下错误的解决方法: 错误描述:请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理 解决方法: 打开cmd命令窗口,执行如下 ...

  10. Android(性能)

    ■ 数据传输 对象和字节流之间的转换 为什么要转? 持久化(装逼说法,JVM非运行的场合),他进程(装逼说法,其他机器JVM,不同的JVM) Parcelable和Serializable 初衷: P ...