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. JavaSE学习笔记(1)---数据类型、运算符、控制结构

    javaSE学习笔记(1) 数据类型和运算符 1.注释可以提高程序的可读性.可划分为 单行注释 // 多行注释 /.../ 文档注释 /**...*/ 2.标识符的命名规则: 标识符必须以字母.下划线 ...

  2. VSCode常用插件之vscode-fileheader使用

    更多VSCode插件使用请访问:VSCode常用插件汇总 vscode-fileheader这是一个给js文件(html.css也可以使用,但是没意义!!!)生成头部注释的插件,每次修改js文件之后会 ...

  3. shell-删除指定时间前的文件

    需要配合find和rm两个命令完成 显示20分钟前的文件: find /home/prestat/bills/test -type f -mmin +20 -exec ls -l {} \; 删除20 ...

  4. 链表问题----删除链表的中间节点和a/b处的节点

    删除链表的中间节点和a/b处的节点 对于给定一个链表的头节点head,实现删除链表的中间节点的函数. 例如 不删除任何节点: 1->2,删除节点1 1->2->3,删除节点2 1-& ...

  5. java 类型判断

    //java 类型匹配测试 Circle circle = new Circle(); // circle rectangle 实现了 shape System.out.println(circle ...

  6. HTML表格显示的笔记

    有时需要显示的复杂表头 如图所示  <table id="" cellpadding="0" cellspacing="0" bord ...

  7. Hibernate的理论知识点

    转自网络 一. 对象持久化的理论 1.对象持久化:内存中的对象转存到外部持久设备上,在需要的时候还可以恢复. 2.对象持久化的原因(目标): 物理: 1) 内存不能持久,需要在硬盘上持久保存 //(物 ...

  8. jdk8-》joining、groupingBy、summarizingInt函数

    拼接函数 Collectors.joining // 3种重载方法 Collectors.joining() Collectors.joining("拼接符") Collector ...

  9. Codeforces Round #600 (Div. 2) A. Single Push

    #include<iostream> #include<cstdio> #include<cstdlib> using namespace std; int T,n ...

  10. c# 嵌入资源并读取

    原文:c# 嵌入资源并读取 1. 右键点击项目,选择资源,然后选择资源的类型,插入资源. 2. 这时候在项目的目录树上会出现一个Resource的文件夹,找到嵌入的资源文件,右击属性,在 Build ...