265. 粉刷房子 II
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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:二叉树下的不能相邻,求能 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- 树莓派使用Python使用PiCamera录制视频
个人博客 地址:https://www.wenhaofan.com/article/20190430144020 代码 在树莓派环境下官方提供了乐酷 PiCamera 让我们可以很方便的操作树莓派连接 ...
- Ueditor1.4.3.3 asp UTF-8版文件缺失修改方法
前几天在做一个项目中需要用富文本编辑器,最终选定用Ueditor编辑器,从官网下载源码(http://ueditor.baidu.com/website/download.html),按照文档创建de ...
- 第十八次CSP认证游记 | 2019.12.15
CSP认证的考试是Haogod介绍的,取得一定成绩之后能有机会参加CCSP的分赛区和全国决赛.这次来参加认证要感谢老师的奔走为我们申请学校的报销,虽然最终因为这不是比赛所以报名费和差旅费下不来,但是老 ...
- 异常处理_python
一.异常处理格式: name=[1,2]data={}try: name[3] data['name']except (KeyError,IndexError) as e : #捕捉指定的几个错误类型 ...
- Wannafly Camp 2020 Day 2E 阔力梯的树 - set,启发式合并
搞一波启发式合并即可 #include <bits/stdc++.h> using namespace std; #define int long long #define iter se ...
- 最近公共祖先 Lowest Common Ancestors
基于深度的LCA算法: 对于两个结点u.v,它们的深度分别为depth(u).depth(v),对于其公共祖先w,深度为depth(w),u需要向上回溯depth(u)-depth(w)步,v需要d ...
- android获取系统信息
连接手机,adb shell 进入 Android Shell 模式,输入 getprop 获取系统属性值 通过上面方法拿到属性名,然后通过下面方法获取到系统的属性值 /** * 获取build.pr ...
- 怎么编写API和或者自己不属于知识小案例demo程序
再使用一个函数,进行测试的调用,然后进行调用这样就不需要再为makefile什么,还有就是改什么鬼东西烦恼了. 然后把需要的功能进行分解就行了.
- [POI2007] OSI-Axes of Symmetry
Description Luogu3454 BZOJ1100 Solution 把边和角顺次放入一个数组里,如果一个点为中心的回文串的半径大于\(n\),那就一定是一个对称轴.跑一遍manacher就 ...
- 当要打开PDB时为何会有Warning: PDB altered with errors.
对PDB执行 alter pluggable database pdbprod2 open; 操作后提示:Warning: PDB altered with errors. 来自AskScuti博客园 ...