[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 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.
Note:
All costs are positive integers.
Example:
- Input: [[1,5,3],[2,9,4]]
- Output: 5
- Explanation: Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5;
- Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5.
Follow up:
Could you solve it in O(nk) runtime?
这道题是之前那道 Paint House 的拓展,那道题只让用红绿蓝三种颜色来粉刷房子,而这道题让用k种颜色,这道题不能用之前那题的解法,会 TLE。这题的解法的思路还是用 DP,但是在找不同颜色的最小值不是遍历所有不同颜色,而是用 min1 和 min2 来记录之前房子的最小和第二小的花费的颜色,如果当前房子颜色和 min1 相同,那么用 min2 对应的值计算,反之用 min1 对应的值,这种解法实际上也包含了求次小值的方法,感觉也是一种很棒的解题思路,参见代码如下:
解法一:
- class Solution {
- public:
- int minCostII(vector<vector<int>>& costs) {
- if (costs.empty() || costs[].empty()) return ;
- vector<vector<int>> dp = costs;
- int min1 = -, min2 = -;
- for (int i = ; i < dp.size(); ++i) {
- int last1 = min1, last2 = min2;
- min1 = -; min2 = -;
- for (int j = ; j < dp[i].size(); ++j) {
- if (j != last1) {
- dp[i][j] += last1 < ? : dp[i - ][last1];
- } else {
- dp[i][j] += last2 < ? : dp[i - ][last2];
- }
- if (min1 < || dp[i][j] < dp[i][min1]) {
- min2 = min1; min1 = j;
- } else if (min2 < || dp[i][j] < dp[i][min2]) {
- min2 = j;
- }
- }
- }
- return dp.back()[min1];
- }
- };
下面这种解法不需要建立二维 dp 数组,直接用三个变量就可以保存需要的信息即可,参见代码如下:
解法二:
- class Solution {
- public:
- int minCostII(vector<vector<int>>& costs) {
- if (costs.empty() || costs[].empty()) return ;
- int min1 = , min2 = , idx1 = -;
- for (int i = ; i < costs.size(); ++i) {
- int m1 = INT_MAX, m2 = m1, id1 = -;
- for (int j = ; j < costs[i].size(); ++j) {
- int cost = costs[i][j] + (j == idx1 ? min2 : min1);
- if (cost < m1) {
- m2 = m1; m1 = cost; id1 = j;
- } else if (cost < m2) {
- m2 = cost;
- }
- }
- min1 = m1; min2 = m2; idx1 = id1;
- }
- return min1;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/265
类似题目:
参考资料:
https://leetcode.com/problems/paint-house-ii/
https://leetcode.com/problems/paint-house-ii/discuss/69509/Easiest-O(1)-space-JAVA-solution
https://leetcode.com/problems/paint-house-ii/discuss/69492/AC-Java-solution-without-extra-space
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Paint House II 粉刷房子之二的更多相关文章
- [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] 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 ...
- [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] Flip Game II 翻转游戏之二
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态
原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...
- LinqToDB 源码分析——DataContext类
LinqToDB框架是一个轻量级的ORM框架.当然,功能上来讲一定比不上Entity Framework的强大.但是在使用上总让笔者感觉有一点Entity Framework的影子.笔者想过可能的原因 ...
- 仓位管理 – 1.理论篇
看到文章标题中的"仓位管理",读者可能会认为它只适用于股市投资.其实不然.只要是投资都涉及到风险.回报率.投资额度,都会涉及到仓位管理.再者,人生本身就带着无数的抉择.风险和回报, ...
- html中,文件上传时使用的<input type="file">的样式自定义
Web页面中,在需要上传文件时基本都会用到<input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多 ...
- ubuntu Chromium 安装 pepperflashplugin
sudo apt-get update sudo apt-get install chromium-browser sudo apt-get install pepperflashplugin-non ...
- Map集合
1:Map (1)将键映射到值的对象. 一个映射不能包含重复的键:每个键最多只能映射到一个值. 键值对的方式存在 (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯 ...
- session & cookie(li)
Session & Cookie 一.定义 Session,用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间.Cookie,由服务器端生成,发送 ...
- POI读取EXCEL(2007以上)
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; im ...
- 商业智能BI推动制造业智能化转型
制造业是我国国民经济的支柱产业,是我国经济增长的主导部门和经济转型的基础,如今我国制造业面临技术工艺不精.缺乏市场意识.商贸流通环节多.物流成本大.仓储效率低下等问题,正处在转型的特殊时期. 内忧: ...
- Spring 4 使用Freemarker模板发送邮件&添加附件
前言 Spring对Java的邮件发送提供了很好的支持,提供了超级简单的API,大大简化了Java邮件发送功能的开发. Spring对Email的支持是基于JavaMail API开发的,所以,我们在 ...