Q:

A:

首先这题可以和粉刷房子这题一样解法,对于i号房子,遍历k种颜色,对于每一种,都去找i-1号房子除该颜色之外的最小花费。但上一题是3种颜色,总复杂度O(N),这题k种颜色,复杂度O(NK^2),题目要求O(NK),则对于i号房子我们保存下当前房子最小的花费以备i+1号房子使用,但因为相邻房子不能涂相同颜色的油漆。假设对于i号房子最小花费是涂x号油漆,则对于i+1号房子来说,一定不能涂x号了。解决办法是我们对于每一个房子,保存最小花费MIN和次最小花费2_MIN。对于下一个房子,涂某种油漆时不能使用MIN,即油漆号等于MIN所对应的油漆号时,此时只能使用2_MIN。除此之外,都使用MIN。

class Solution {
public:
int minCostII(vector<vector<int>>& costs) {
if(costs.size()==0 or costs[0].size()==0){
return 0;
}
int pre_min_index=-1,pre_min_cost=0,pre_2min_cost=0;
for(int i=0;i<costs.size();++i){
int cur_min_index,cur_min_cost=INT_MAX,cur_2min_cost=INT_MAX;
for(int j=0;j<costs[0].size();++j){
if(j!=pre_min_index){
costs[i][j]+=pre_min_cost;
}
else{
costs[i][j]+=pre_2min_cost;
}
if(costs[i][j]<cur_min_cost){
cur_2min_cost=cur_min_cost;
cur_min_cost=costs[i][j];
cur_min_index=j;
}
else if(costs[i][j]<cur_2min_cost){
cur_2min_cost=costs[i][j];
}
}
pre_min_index=cur_min_index;
pre_min_cost=cur_min_cost;
pre_2min_cost=cur_2min_cost;
// cout<<pre_min_index<<" "<<pre_min_cost<<" "<<pre_2min_cost<<endl;
}
return pre_min_cost;
}
};

265. 粉刷房子 II的更多相关文章

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

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

  4. [Swift]LeetCode256.粉刷房子 $ Paint House

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

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

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

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

  9. [LeetCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

随机推荐

  1. MATLAB用“fitgmdist”函数拟合高斯混合模型(一维数据)

    MATLAB用“fitgmdist”函数拟合高斯混合模型(一维数据) 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 在MATLAB中“fitgmdis ...

  2. 如何查看oracle当前连接数,会话数

    第一步,在cmd命令行,输入sqlplus 第二步,根据提示输入用户名与密码 1. 查看processes和sessions参数 SQL> show parameter processes NA ...

  3. Bootstrap 警告框(Alert)插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. linux 配置compoer

    配置默认php 删除 rm -f /usr/bin/php 改到php7.3版本的composer /bin/php /usr/bin/php 多版本支持 配置php7专用composer70 cd ...

  5. Excel如何快速选定所需数据区域

    在使用Excel处理数据时,快速选定所需数据区域的一些小技巧. 第一种方法:(选定指定区域) Ctrl+G调出定位对话框,在[引用位置]处输入A1:E5000,点击[确定]即可.  第二种方法:(选定 ...

  6. 牛客CSP-S提高组赛前集训营3 赛后总结

    货物收集 二分答案.复杂度\(O(n\log n)\). 货物分组 用费用提前计算的思想,考虑用一个新的箱子来装货物会发生什么. 显然费用会加上后面的所有货物的总重. \(60\)分的\(O(n^2) ...

  7. H5手机端开发问题及解决方案

    ios竖屏拍照上传,图片被旋转问题 1.通过第三方插件exif-js获取到图片的方向2.new一个FileReader对象,加载读取上传的图片3.在fileReader的onload函数中,得到的图片 ...

  8. 温故知新的经典贪心题目:今年暑假不AC?

    情景: “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电 ...

  9. LINUX使用SSH远程终端时,如何将运行时间长的程序在后台挂起,下次SSH登陆时继续使用同一个SHELL?

    我在某个平台上购买了一个云服务器,LINUX操作系统无图形化界面,硬盘空间较小.虽然在平台上可以通过其自带网页版VNC界面登陆SHELL进而操控云主机,但是每次需要操控都得打开网页登陆进平台,然后再进 ...

  10. Docker学习(六)Dockerfile构建自定义镜像

    Docker学习(六)Dockerfile构建自定义镜像 前言 通过前面一篇文章可以知道怎么去使用一个镜像搭建服务,但是,如何构造自己的一个镜像呢,docker提供了dockerfile可以让我们自己 ...