Paint House II 解答
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 解答的更多相关文章
- [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 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:二叉树下的不能相邻,求能 ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [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 ...
- 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 ...
- [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 ...
- Word Pattern II 解答
Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...
- [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 ...
随机推荐
- [每日一题] 11gOCP 1z0-053 :2013-09-30 ASMCMD.......................................................8
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12206095 正确答案:BCD 为了使ASM文件管理更简单,Oracle提供了一个命令实用 ...
- storm高级原语-Transactional topology
参考: http://xumingming.sinaapp.com/736/twitter-storm-transactional-topolgoy/ http://xumingming.sinaap ...
- MyBatis的简单操作
这里将的是简单的增.删.改.查等基本操作 首先创建java项目,导入相应的依赖包,这里可以参考上一篇博客 1.添加数据 在jike.book.pojo包中,新建java类 JiKeUser.java: ...
- shell中的替换
shell中如果存在一些特殊的字符,就需要进行替换,可进行命令替换.变量替换.转义替换 1.转义字符的替换 shell中包含以下的转移字符 \a 响铃警报\\ 反斜杠 \b 退格(删除键) ...
- sqlserver 2008 sa登陆的一些问题
sqlserver 2008 sa登陆 无法连接到(local)? 遇到这个问题请确保SQL主服务是开启状态: ok接下来把服务器名换成 计算机名\实例名 再次使用sa登陆,如下: 是不是可以了呢? ...
- Linux ssh安全设置
本文摘要 SSH服务器配置文件是/etc/ssh/sshd_conf.在你对它进行每一次改动后都需要重新启动SSH服务,以便让改动生效. SSH服务器配置文件是/etc/ssh/sshd_ ...
- 内容提供者 ContentResolver 数据库 示例 -2
MainActivity public class MainActivity extends ListActivity { // 访问内容提供者时需要的主机名称 public stat ...
- 代码段编辑器SnippetEditor 2.1
1.选择程序版本 2.可以创建文件夹 3.新建片段 4.给片段取名 5.双击进行编辑 6.点击保存 7.直接使用
- OD: Writing Small Shellcode
第 5.6 节讲述如何精简 shellcode,并实现一个用于端口绑定的 shellcode.原书中本节内容来自于 NGS 公司的安全专家 Dafydd Stuttard 的文章 “Writing S ...
- pd的django To do list 教程------(1)说明与展示
1:开发环境:windows7+django1.8+Python2.7+mysql数据库 2:开发工具:pycharm 3:说明与展示 以上就是最后的页面,可以完成添加,删除,编辑,已完成(勾选che ...