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

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

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

  3. [Locked] Meeting Room I && II

    Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...

  4. [Locked] Flip Game I & II

    Flip Game I You are playing the following Flip Game with your friend: Given a string that contains o ...

  5. [Locked] Palindrome Permutation I & II

    Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...

  6. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  7. sql语句优化总结

    sql语句优化总结 数据库优化的几个原则: 1.尽量避免在列上做运算,这样会导致索引失败: 2.使用join是应该用小结果集驱动大结果集,同时把复杂的join查询拆分成多个query.不然join的越 ...

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

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

随机推荐

  1. CSS3 box-sizing 属性

    定义和用法 box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box& ...

  2. asp.net模态窗口返回值

    个人感觉模态窗口在做网站的时候,使用到的比较少,前段时间在做项目时要实现以模态窗口传值和接收返回值, 模态窗口传值实现比较简单,但是做好后发现在Chrome浏览器中接收不到返回值,修改好Chrome浏 ...

  3. jquery选择器的使用方式

    1.基本选择器   选择器 描述 返回 示例 代码说明 1 id选择器 根据指定的id匹配元素 单个元素 $("#one").css("background", ...

  4. ios8及以前的特性

    目前最新系统为ios8.以下为历代系统的回顾: iOS 1 关键词:iPhone的诞生 也许放在现在来看,当时的情景很难想象.当第一代iPhone正式发布时,在某些功能和方面其实是要远远落后于当时的竞 ...

  5. SOAPUI请求及mockservice 使用

    1.新建soap Project,输入wsdl的地址,运行request                                                               2 ...

  6. Qt Quick 与 QML语言(初学笔记1)

    Qt Quick Qt Quick是一些新的UI技术的集合,用来帮助开发者创建一种现在越来越多用于手机.多媒体播放器.机顶盒以及其他便携式设备上的直观的.现代的.流畅的用户界面.简单来说,Qt Qui ...

  7. TestNG目录

    1 - 简介  2 - 注解  3 - testng.xml  4 - 执行 TestNG  5 - 测试方法, 测试类 和 测试组    5.1 - 测试方法    5.2 - 测试组    5.3 ...

  8. linux之uniq

    Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...

  9. 为什么我们不喜欢用富UI控件

    我们对于理解一般意义的抽象关系并没有问题,但如第一部分使用Entity Framework 时说明的,事实恰好相反.我们还喜欢在交付应用程序时利用抽象关系,比如使用Azure等云服务.在这两种情况下, ...

  10. SQL2012之FileTable与C#的联合应用

    关于FileTable是什么,请猛击如下链接:http://technet.microsoft.com/zh-cn/library/ff929144(v=SQL.110).aspx:如您已知道,请跳过 ...