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 ...
随机推荐
- 【Struts2】进阶
一.Action处理请求参数 1.1 属性驱动 1.2 模型驱动 1.3 扩展 将数据封装到List集合 将数据封装到Map集合 二.类型转换 2.1 自定义类型转换器: 1.创建一个自定义类型转换器 ...
- 【Day2】4.第三方模块的安装与使用
课程目标 1. 使用模块 2. 安装第三方模块 使用模块 • 一个.Py文件称之为一个模块(Module) • 好处: 1. 便于代码维护,把很多函数放到不同文件,一个.py文件 的 代码数量少 2. ...
- C# List集合去除重复数据
实例如下: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; ...
- shell 中的通配符:
shell 中的通配符: *: 代表 0 个或者多个任意字符 ?: 代表一定有一个的任意字符 []: 代表一定有一个在括号内的字符(非任意字符).例如[abcd]代表一定有一个字符,可能是 abcd ...
- MongoDB安全运维
0×00 MongoDB权限介绍 1.MongoDB安装时不添加任何参数,默认是没有权限验证的,登录的用户可以对数据库任意操作而且可以远程访问数据库,需以–auth参数启动. 2.在刚安装完毕的时候M ...
- ServiceLoader在SPI中的重要作用分析
对于线程上下文类加载器在之前已经对它进行了详细的理论化的学习,其中对于这个类加载器应用最多的也就是在SPI场合下用来打破双亲委托机制,如之前所描述的: 这次举一个具体的例子来进一步的加深对线程上下文类 ...
- oracle时间差计算
1.months_between(date1,date2);date1和date2相减得到相差的月份. select months_between(to_date('2015-05-11','yyyy ...
- 使用ADB命令写Android自动化测试脚本
使用脚本来执行测试的特点: ●书写方便 ●基本上可以实现90%以上的功能性覆盖 ●测试结果需要通过自己观察整个过程和日志文件来得出的 ●有些外部的动作,脚本是无法实现的,比如录入指纹 ●只适配特定尺寸 ...
- vue 后退不刷新,前进刷新 keep-alive
最近在开发中遇到了这样的一个问题: A.B.C三个页面,有如下这样的场景: (1)从页面A离开进入B或C的时候,缓存A页面的数据,并且返回到A后,能保持A页面的跳转前职位 (2)离开B进入C的时候,缓 ...
- ACM-ICPC 2017 南宁赛区现场赛 M. The Maximum Unreachable Node Set(二分图)
题目链接:https://nanti.jisuanke.com/t/19979 题意:给出一个 n 个点,m 条边的 DAG,选出最大的子集使得其中结点两两不能到达. 题解:参考自:https://b ...