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. Deep Clustering Algorithms

    Deep Clustering Algorithms 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 本文研究路线:深度自编码器(Deep Autoen ...

  2. 22.01.Cluster

      1. 클러스터링 iris 데이터셋 확인¶ In [2]: from sklearn import cluster from sklearn import datasets iris = dat ...

  3. 小程序图片上传,长按删除,weui

    <view class="weui-cells"> <view class="weui-cell"> <view class=&q ...

  4. Linux运维---16. Kolla部署OpenStack外接rabbit集群

    # 前提时rabbit集群已经搭建完成 vim /etc/kolla/globals.yml enable_rabbitmq: "no" rpc_transport_url: &q ...

  5. sql 分组后查询出排序字段

    select  row_number() over(partition by  CODE order by SEQUENCE) as RowIndex  from Table 注:根据表的CODE 字 ...

  6. C语言回文链表

    要求:请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true思路:利用快慢双指针+反转半链 ...

  7. [MongoDB] 使用PHP在MongoDB中搜索的实现

    条件操作符用于比较两个表达式并从mongoDB集合中获取数据.MongoDB中条件操作符有:(>) 大于 - $gt(<) 小于 - $lt(>=) 大于等于 - $gte(< ...

  8. Java自学-Lambda 概念

    Java Lambda 表达式概念 假设一个情景: 找出满足条件的Hero 从使用普通方法,匿名类,以及Lambda这几种方式,逐渐的引入Lambda的概念 步骤 1 : 普通方法 使用一个普通方法, ...

  9. windows redis启动

    1.下载redis 2.启动redis 3.启动redis客户端并设置protected-mode为false

  10. 安装python包时遇到"error: Microsoft Visual C++ 9.0 is required"的简答(Python2.7)

    简答 在Windows下用pip安装Scrapy报如下错误, error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall ...