[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 ...
随机推荐
- 以实际的WebGIS例子探讨Nginx的简单配置
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 以实际项目中的一个例子来详细讲解Nginx中的一般配置,其中涉 ...
- Docker for Windows使用简介
在上一篇文章中,通过演练指导的方式,介绍了在Docker中运行ASP.NET Core Web API应用程序的过程.本文将介绍Docker for Windows的使用. 先决条件 前两周时间,Do ...
- 使用 Windows Phone Toolkit 的 Tilt 效果
上一篇文章分享了如何使控件具有摁下的效果(在WindowsPhone中使控件具有Tilt效果),实现方式是在项目中添加新的类文件,其实,如果项目引用了Windows Phone Toolkit,那么就 ...
- PhpStorm集成xdebug进行断点调试
本文介绍如何使用PhpStorm集成xdebug在本地开发环境进行断点调试的技巧. 我配置的环境是:Windows10 + PhpStorm10.0.1 + PHP5.6. 1. 下载xdebug的扩 ...
- iOS UITableViewableViewCell自适应高度
前两天做了一个项目,中间有遇到一个问题,就是聊天的时候cell高度的问题.这是一个很多前辈都遇到过,并且很完美的解决过的问题.这里主要是记录自己的学习心得.项目中首先想到的是用三方库,可是有问题,遂放 ...
- jquery获取dropdownlist的value和text值
1.jquery //获取value值 $("#ddlSubmodel").val(); //获取text值 $("#ddlSubmodel").find(&q ...
- IOS 2D游戏开发框架 SpriteKit-->续(创建用户角色精灵--原创)
一.主要实现 今天spritekit实现创建玩家角色精灵(SKSpriteNode *), 增加角色精灵的手势操作,这里增加的手势计算方法与objective-c中是不一样的,因为objectiv ...
- HTTP各状态码解释
状态码 含义 100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应.服务器必 ...
- MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法 和sql片段)(三)
一.字段名与属性名(数据库的名字)不一样怎么办? 方案一:在小配置中配置一个resultMapper <!--方案一:resultMapper 字段名与属性名不一致 --> <res ...
- SAP CRM 通过调试观察CL_CRM_BOL_ENTITY中的数据
这个(BOL里面)最重要的类值得一看. BOL中的每条记录都会在CL_CRM_BOL_ENTIT中表示.至今,我们已经写过一些事件处理器,并且我们已经直接或间接的通过这个类工作.在业务场景中,我们也许 ...