256. Paint House (Easy)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 3
cost matrix. For example, costs[0][0]
is the cost of painting house 0 with color red; costs[1][2]
is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.
Note:
All costs are positive integers.
Solution 1: DP 9ms
这道题说有n个房子,每个房子可以用红绿蓝三种颜色刷,每个房子的用每种颜色刷的花费都不同,限制条件是相邻的房子不能用相同的颜色来刷,现在让我们求刷完所有的房子的最低花费是多少。这题跟House Robber II和House Robber很类似,不过那题不是每个房子都抢,相邻的房子不抢,而这道题是每个房子都刷,相邻的房子不能刷同一种颜色。而Paint Fence那道题主要考察我们有多少种刷法,这几道题很类似,但都不一样,需要我们分别区分。但是它们的解题思想都一样,需要用动态规划Dynamic Programming来做,这道题我们需要维护一个二维的动态数组dp,其中dp[i][j]
表示刷到第i+1房子用颜色j的最小花费,递推式为:
dp[i][j] = dp[i][j] + min(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]);
这个也比较好理解,如果当前的房子要用红色刷,那么上一个房子只能用绿色或蓝色来刷,那么我们要求刷到当前房子,且当前房子用红色刷的最小花费就等于当前房子用红色刷的钱加上刷到上一个房子用绿色和刷到上一个房子用蓝色的较小值,这样当我们算到最后一个房子时,我们只要取出三个累计花费的最小值即可,参见代码如下:
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[0].empty()) return 0;
vector<vector<int>> dp = costs;
for (int i = 1; i < costs.size(); ++i) {
for (int j = 0; j < 3; ++j) {
dp[i][j] += min(dp[i-1][(j+1)%3], dp[i-1][(j+2)%3]);
}
}
return min(min(dp.back()[0], dp.back()[1]), dp.back()[2]);
}
};
version 2: without extra space 6ms
从零开始loop!
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[0].empty()) return 0;
int min1 = 0, min2 = 0, idx = -1;
for (int i = 0; i < costs.size(); ++i) {
int m1 = INT_MAX, m2 = INT_MAX, id1 = -1;
for (int j = 0; j < 3; ++j) {
int cost = costs[i][j] + (j == idx ? min2:min1);
if (cost < m1) {
m2 = m1;
m1 = cost;
id1 = j;
} else if (cost < m2) {
m2 = cost;
}
}
min1 = m1, min2 = m2, idx = id1;
}
return min1;
}
};
Solution 2: DP 6ms
由于只有红绿蓝三张颜色,所以我们就可以分别写出各种情况,这样写可能比上面的写法更加一目了然一些,更容易理解一点吧:
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[0].empty()) return 0;
vector<vector<int>> dp = costs;
for (int i = 1; i < costs.size(); ++i) {
dp[i][0] += min(dp[i-1][1],dp[i-1][2]);
dp[i][1] += min(dp[i-1][0],dp[i-1][2]);
dp[i][2] += min(dp[i-1][0],dp[i-1][1]);
}
return min(min(dp.back()[0], dp.back()[1]), dp.back()[2]);
}
};
version 2: without extra space
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[0].empty()) return 0;
int v0 = costs[0][0], v1 = costs[0][1], v2 = costs[0][2];
for (int i = 1; i < costs.size(); ++i) {
int m0 = costs[i][0]+min(v1,v2);
int m1 = costs[i][1]+min(v0,v2);
int m2 = costs[i][2]+min(v0,v1);
v0 = m0, v1 = m1, v2 = m2;
}
return min(min(v0,v1), v2);
}
};