[Locked] Paint House I & II
Paint House
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 3
cost matrix. For example, costs[0][0]
is the cost of painting house 0 with color red; costs[1][2]
is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.
Note:
All costs are positive integers.
分析:
典型动态规划,通过遍历所有情况可以弥补前面的选择对后面的影响。时间复杂度为O(n*3*3) = O(n);利用滚动数组,空间复杂度为O(3*2) = O(1)。
代码:
int minCost2(vector<vector<int> > cost) {
vector<int> opt(, ), temp(, );
for(int i = ; i < cost.size(); i++) {
for(int j = ; j < ; j++) {
temp[j] = INT_MAX;
for(int k = ; k < ; k++) {
if(j != k)
temp[j] = min(temp[j], opt[k] + cost[i][j]);
}
}
opt.swap(temp);
}
int minc = INT_MAX;
for(int i : opt)
minc = min(minc ,i);
return minc;
}
Paint House II
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?
分析:
如果采用I中的方法,时间复杂度为O(n*k*k),空间复杂度为O(k*2)。为了降低时间复杂度,可以通过减少两重k循环里的大量重复计算来使得O(k*k)的复杂度变为O(k)。此题中,对于j = j1, j2两种情况,它们的内部k循环有k-2次是重复比较了cost[i][j1] + x和cost[i][j2] + x的大小的,可以通过一次cost[i][j1]和cost[i][j2]的比较替代;扩展到j = 1...k种情况,只需找到小的cost[i][j], j = 1...k.
解法:
动态规划,第i轮的最小代价是j = j1时,假设第i + 1轮中,j = j1是默认剔除的,很简单,前一轮的结果后一轮的最小结果都应该使用的,那么第i + 1轮的最小代价是min(cost[i+1][j])的j的取值时;然而j = j1并不是默认剔除的,故在第i + 1轮是cost[i + 1][j1]是无法使用上一轮的最小结果的,但它应该使用第二小的结果,故只需要比较第i + 1轮中,j == j1和j != j1两种情况的值即可。时间复杂度为O(n*(k + 常数)) = O(nk),空间复杂度,利用滚动值存储中间结果,为O(1)
代码:
int minCost(vector<vector<int> > cost) {
int min1 = , min2 = , record = -, c = INT_MAX;
for(int i = ; i < cost.size(); i++) {
int minval1 = INT_MAX, minval2 = INT_MAX, last = -;
for(int j = ; j < cost[].size(); j++) {
if(record != j) {
if(minval1 > cost[i][j]) {
minval2 = minval1;
minval1 = cost[i][j];
last = j;
}
else
minval2 = min(minval2, cost[i][j]);
}
}
int a = minval1 + min1, b = minval2 + min1;
if(record != -)
c = cost[i][record] + min2;
if(a < c) {
record = last;
min1 = a;
min2 = min(b, c);
}
else {
min1 = c;
min2 = a;
} }
cout<<endl;
return min1;
}
[Locked] Paint House I & II的更多相关文章
- [LeetCode] Paint House I & II
Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...
- [Locked] Paint Fence
Paint Fence There is a fence with n posts, each post can be painted with one of the k colors. You ha ...
- [Locked] Meeting Room I && II
Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...
- [Locked] Flip Game I & II
Flip Game I You are playing the following Flip Game with your friend: Given a string that contains o ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- sql语句优化总结
sql语句优化总结 数据库优化的几个原则: 1.尽量避免在列上做运算,这样会导致索引失败: 2.使用join是应该用小结果集驱动大结果集,同时把复杂的join查询拆分成多个query.不然join的越 ...
- [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:二叉树下的不能相邻,求能 ...
随机推荐
- php中文乱码
一. 首先是PHP网页的编码 1. php文件本身的编码与网页的编码应匹配 a. 如果欲使用gb2312编码,那么php要输出头:header(“Content-Typ ...
- system.getProperties()
Properties props=System.getProperties(); //系统属性 System.out.println("Java的运行环境版本:"+prop ...
- spl_autoload_register()和__autoload()
关于spl_autoload_register()和__autoload() 看两者的用法: //__autoload用法 function __autoload($classname) { ...
- Spring 整合Redis 出现 afterPropertiesSet signature: ()V) Incompatible argument to function 解决办法
正在做SpringMVC+Redis整合的练习 使用的是 spring-data-redis 和 Jedis 配置好之后出现了以下错误: Caused by: java.lang.VerifyErro ...
- CSS3 :nth-child() 选择器
CSS3 :nth-child() 选择器 代码: <!DOCTYPE html> <html> <head> <style> p:nth-child( ...
- Android 内核基本知识
Android基本知识 Android基本知识.... 1 1. 各版本系统特性.... 1 2. View绘制流程.... 2 3. 动画体系.... 2 4. 事件分发机制.... 3 输入消息获 ...
- Java笔记原生数据类型【二】
1.Java中的数据类型分为: 1)原生类型(primitive Data type) 2.)引用类型(对象类型) (Reference Type) 1.变量和常量: 常量: 就是值不会变化的量: 变 ...
- Solr4.8.0源码分析(25)之SolrCloud的Split流程
Solr4.8.0源码分析(25)之SolrCloud的Split流程(一) 题记:昨天有位网友问我SolrCloud的split的机制是如何的,这个还真不知道,所以今天抽空去看了Split的原理,大 ...
- 转:php中实现精确设置session过期时间的方法
原文来自于:http://www.jb51.net/article/52309.htm 大多数据情况下我们对于session过期时间使用的是默认设置的时间,而对于一些有特殊要求的情况下我们可以设置一下 ...
- 教你在Java的普通类中轻松获取Session以及request中保存的值
曾经有多少人因为不知如何在业务类中获取自己在Action或页面上保存在Session中值,当然也包括我,但是本人已经学到一种办法可以解决这个问题,来分享下,希望对你有多多少少的帮助! 如何在Java的 ...