LC 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 painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
The cost of painting each house with a certain color is represented by a n x k
cost matrix. For example, costs[0][0]
is the cost of painting house 0 with color 0; costs[1][2]
is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.
思路:DP,第n个house如果paint color i, 那第n-1个house就不能。
注意minval1和minval2是前一次dp的最小值,而不是前一个costs的最小值。
class Solution {
public:
int minCostII(vector<vector<int>>& costs) {
if (costs.size() == || costs[].size() == ) return ;
int N = costs.size();
int K = costs[].size();
vector<vector<int>> dp(N, vector<int>(K, ));
for (int i = ; i < K; i++) dp[][i] = costs[][i];
for (int i = ; i < N; i++) {
int minval1 = INT_MAX, minval2 = INT_MAX;
int minidx1 = , minidx2 = ;
for (int j = ; j < K; j++) {
if (minval1 > dp[i-][j]) {
minval1 = dp[i-][j];
minidx1 = j;
}
}
for (int j = ; j < K; j++) {
if (minval2 > dp[i-][j] && j != minidx1) {
minval2 = dp[i-][j];
minidx2 = j;
}
}
for (int j = ; j < K; j++) {
if (minidx1 == j) dp[i][j] = costs[i][j] + minval2;
else dp[i][j] = costs[i][j] + minval1;
}
}
int minval = INT_MAX;
for (int i = ; i<K; i++) {
minval = min(minval, dp[N-][i]);
}
return minval;
}
};
LC 265. Paint House II的更多相关文章
- 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:二叉树下的不能相邻,求能 ...
- 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 ...
- 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 pai ...
- [LeetCode#265] Paint House II
Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- LeetCode Paint House II
原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...
随机推荐
- CHD-5.3.6集群上sqoop安装
Sqoop(发音:skup)是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL ...
- hadoop中hive的属性
1.在hive中是可以删除文件的: hive> dfs -rm -R /u2.txt > ; Deleted /u2.txt 2.hive 中的default数据库 <propert ...
- IDM下载百度资源出现403的解决方法
测试发现是受cookie的影响,百度为了防止用外部下载工具突破限速加入了cookie验证,因为一般的下载工具请求下载的时候不会附加cookie信息. IDM就是这样,它请求下载文件时只知道文件的下载地 ...
- 【python】导入自定义模块
一.直接import 1.当执行文件与要导入的py文件在同一目录下时 假设要在wangyi.py中导入weibo.py文件 import weibo 2.当执行文件与要导入的py文件所在文件夹在同一目 ...
- redis——搭建
https://blog.csdn.net/sinat_29699167/article/details/79699200 Django使用Redis进行缓存详细最全流程 https://blog.c ...
- 在idea中相同的字符串使用equals()进行比较时,返回值是flase问题
最近在idea中遇到了一个编码的问题,我的程序是从前台传过来一个字符串,判断用户的角色(学生,教师,管理员), 在进行equals()判断时,返回的确是false,然后就在网上查了查,发现是编码的问题 ...
- python : import详解。
用户输入input() input()函数: 用于从标准输入读取数值. >>> message = input('tell me :') tell me :hahah >> ...
- linux基础_用户组的管理
1.创建组 语法:groupadd 组名 2.删除组 语法:groupdel 组名 3.创建用户时,直接指定组 语法:useradd -g 用户组 用户名 4.修改用户的组 语法:usermod -g ...
- UVa1048 Low Cost Air Travel——最短路
很好的一道题呀 思路 状态\(d(i,j)\)表示已经经过了行程单中的\(i\)个城市,目前在城市\(j\)的最小代价,直接建边跑最短路就行了 比如机票为\(ACBD\),行程单为\(CD\),那么对 ...
- 一个restframework快速实例
首先在settings.py中引入 INSTALLED_APPS = [ ...... 'rest_framework', ......] 相关模型结构如下: class custume(models ...