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. Pikachu-php反序列化

    在理解这个漏洞前,你需要先搞清楚php中serialize(),unserialize()这两个函数. 序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象 ...

  2. (一)Python模块化编程简介

    1 引言 众所周知,模块化编程具备很多优点,尤其在复杂项目上体现更为明显.Python模块化编程有助于开发者统筹兼顾和分工协作,并提升代码灵活性和可维护性,是编程开发者不可或缺的一项重要工具. 2 P ...

  3. Mysql分库分表导出导入和数据量统计测试

    需求:添加创建了分库分表后,业务可能将数据已经写入,但未来得及接入到otter汇总库.接入汇总库前需要初始化这部分数据. 1.导出 ip_port_list ) len=${#ip_port_list ...

  4. TensorFlow入门(常量变量及其基本运算)

    1.tensorflow常量变量的定义 测试代码如下: # encoding:utf-8 # OpenCV tensorflow # 类比 语法 api 原理 # 基础数据类型 运算符 流程 字典 数 ...

  5. HTTPClient模拟Get和Post请求

    一.模拟Get请求(无参) 首先导入HttpClient依赖 <dependency> <groupId>org.apache.httpcomponents</group ...

  6. MySql 存储大量长字节 Text报错处理办法

    今天线上版本的错误: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Row size too large ...

  7. Supervision meeting with Liu

    data stream;streampipe/nifi data flow:1. algorithm;;;; top-k pattern, motif and so on 2. implication ...

  8. java序列化与反序列化的使用

    个人博客 地址:http://www.wenhaofan.com/article/20180925214701 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字 ...

  9. Codeforces1295D. Same GCDs (欧拉函数)

    https://codeforces.com/contest/1295/problem/D 设gcd(a,m)= n,那么找gcd(a +x ,m)= n个数,其实就等于找gcd((a+x)/n,m/ ...

  10. int*v=newint[src.cols*4]

    在学习:使用OpenCV2.x计算图像的水平和垂直积分投影中,有下图一种代码: 对比上面两个代码对于同一张图片求得的结果会发现不同: 为什么会出现这个原因呢?不知道为啥这样初始化? 首先查看一下图片深 ...