[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 ...
随机推荐
- 解决Bash On Ubuntu On Window安装Zsh无效问题附安装说明
前言 Zsh是一款非常棒的Shell,使用Linux和Mac系统的人,基本上都知道zsh的存在. 问题 在安装完Zsh后,zsh是可以使用的,但是重启之后,又恢复至默认的bash. 我在安装好之后,使 ...
- VNC软件的安装及使用方法说明
本篇仅为作业... 实验课程:Linux系统 指导老师:刘臣奇 实验机器:联想y410p 实验时间:2016年9月11日 学生学号:140815 姓名:杨文乾 在一台机器安装viewer的同时,在另一 ...
- malloc 与 free函数详解<转载>
malloc和free函数详解 本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...
- jquery获取table的行数、列数
$("#grd").find("tr").length; //行数 $("#grd").find("tr").find( ...
- 设计模式(十)组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- Linux下安装Redis
1. 下载最新版本的Redis源代码: 命令:wget http://download.redis.io/redis-stable.tar.gz 2. 解压并编译 命令:tar xzf redis-s ...
- 最大半连通子图 bzoj 1093
最大半连通子图 (1.5s 128MB) semi [问题描述] 一个有向图G = (V,E)称为半连通的(Semi-Connected),如果满足:∀ u, v ∈V,满足u->v 或 v - ...
- 让linux开机默认开启小键盘
linux默认开机不开启数字键盘numberlock,每次输入开机密码还得劳烦自己去点亮指示灯,让此灯开机自动点亮,需要一个软件才行,就是numlockx了,可以通过yum安装:yuminstall ...
- 数据库---实验四 oracle的安全性和完整性控制
实验内容: (一) 授权 . 以dba用户的身份登陆oracle,创建用户u1+学号后四位,u2+学号后四位. SQL> create user u1_3985 identified by &q ...
- Linux文件管理命令笔记
more\less:翻页命令 more:翻页的形式查看文件内容.该命令可作为管道命令. 翻页过程可使用的键: 空格(space):向下翻页: 回车(Enter):向下翻一行: b:往回翻,只限,但管道 ...