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 ...
随机推荐
- SmartBinding with kbmMW #4
前言 在前面写过的文章中,详细介绍过如何将各种的控件与数据源进行绑定(Bind).在这篇文章中,将重点讨论如何绑定TImage和TListView.(同时支持VCL与Firemonkey). 将图形数 ...
- Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection(Author's Accepted Manuscript)——2014.08
翻译 一项关于视觉特征检测的最新进展概述(作者已被接受的手稿) 和A survey of recent advances in visual feature detection——2014.08内容相 ...
- AJAX配置csrf
// 从COokie取CSRF TOKEN的值 function getCookie(name) { var cookieValue = null; if (document.cookie & ...
- 【cli命令集】
1.调高bug等级 查看现在蜂窝目前sla情况 ROUTER> enable ROUTER# core set debug 18Core debug was 0 and is now 18Cor ...
- PAT Basic 1095 解码PAT准考证 (25 分)
PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...
- Some ML Tutorials
VAE: What-is-variational-autoencoder-vae-tutorial Variational-autoencoders-explained Building variat ...
- grep匹配命令
关于匹配的实例: 统计所有包含“48”字符的行有多少行 grep -c "48" demo.txt 不区分大小写查找“May”所有的行) grep -i "May&q ...
- GET /static/plugins/bootstrap/css/bootstrap.css HTTP/1.1" 404 1718
引用的Bootstrap一直不出来,页面中的静态资源无法加载, 报这个错的原因,是因为配置setting时候没有配置好. 后面在setting里面添加下面这段就好了 STATICFILES_DIRS ...
- mongodb命令----批量更改文档字段名
因为mongodb基于javascript的特性,为了体验cursor的威力我们不妨利用js的for循环创建记录 先创建文档 db.createCollection("columnsampl ...
- 【bzoj3083】遥远的国度(树链剖分+线段树)
题目描述 zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn完成 ...