265 Paint House II 粉刷房子 II
Description:
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 an n x k cost matrix costs.
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...
Return the minimum cost to paint all houses.
Example:
Example 1:
Input: costs = [[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.
Example 2:
Input: costs = [[1,3],[2,4]]
Output: 5
Constraints:
costs.length == n
costs[i].length == k
1 <= n <= 100
2 <= k <= 20
1 <= costs[i][j] <= 20
題目描述:
假如有一排房子共有 n 幢,每個房子可以被粉刷成 k 種顏色中的一種。房子粉刷成不同顏色的花費成本也是不同的。你需要粉刷所有的房子並且使其相鄰的兩個房子顏色不能相同。
每個房子粉刷成不同顏色的花費以一個 n x k 的矩陣表示。
例如,costs[0][0] 表示第 0 幢房子粉刷成 0 號顏色的成本;costs[1][2] 表示第 1 幢房子粉刷成 2 號顏色的成本,以此類推。
返回 粉刷完所有房子的最低成本 。
示例:
示例 1:
輸入: costs = [[1,5,3],[2,9,4]]
輸出: 5
解釋:
將房子 0 刷成 0 號顏色,房子 1 刷成 2 號顏色。花費: 1 + 4 = 5;
或者將 房子 0 刷成 2 號顏色,房子 1 刷成 0 號顏色。花費: 3 + 2 = 5.
示例 2:
輸入: costs = [[1,3],[2,4]]
輸出: 5
提示:
costs.length == n
costs[i].length == k
1 <= n <= 100
2 <= k <= 20
1 <= costs[i][j] <= 20
思路:
- 動態規劃
設 dp[i][j] 表示粉刷 costs[i][j] 的最小累計成本
dp[i][j] = min(costs[i][j] + dp[i - 1][k] for k in range(n) if j != k)
即取遍所有上一行到值中取不與當前行相同的最小值
注意到當前值只和上一行相關, 可以將空間複雜度優化爲 O(n)
時間複雜度爲 O(mn ^ 2), 空間複雜度爲 O(n) - 動態規劃優化
注意到只需要求最值
那麼只要記錄上一行到最小值, 以及第二小的值
當這一行的值和上一行最小值位置相同時, 使用第二小的值
否則使用最小值進行更新
同樣可以使用滾動數組優化空間複雜度
時間複雜度爲 O(mn), 空間複雜度爲 O(n)
代碼:
C++:
class Solution
{
public:
int minCostII(vector<vector<int>>& costs)
{
int m = costs.size(), n = costs.front().size(), first = INT_MAX, second = INT_MAX;
vector<int> dp(costs.front());
for (int j = 0; j < n; j++)
{
if (dp[j] < first)
{
second = first;
first = dp[j];
}
else if (dp[j] < second) second = dp[j];
}
for (int i = 1; i < m; i++)
{
vector<int> cur(n);
int first_cur = INT_MAX, second_cur = INT_MAX;
for (int j = 0; j < n; j++)
{
int pre = dp[j] != first ? first : second;
cur[j] = pre + costs[i][j];
if (cur[j] < first_cur)
{
second_cur = first_cur;
first_cur = cur[j];
}
else if (cur[j] < second_cur) second_cur = cur[j];
}
dp = cur;
first = first_cur;
second = second_cur;
}
return *min_element(dp.begin(), dp.end());
}
};
Java:
class Solution {
public int minCostII(int[][] costs) {
int m = costs.length, n = costs[0].length, dp[] = costs[0], first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
if (dp[j] < first) {
second = first;
first = dp[j];
} else if (dp[j] < second) second = dp[j];
}
for (int i = 1; i < m; i++) {
int cur[] = new int[n], firstCur = Integer.MAX_VALUE, secondCur = Integer.MAX_VALUE;
for (int j = 0; j < n; j++) {
int pre = dp[j] != first ? first : second;
cur[j] = pre + costs[i][j];
if (cur[j] < firstCur) {
secondCur = firstCur;
firstCur = cur[j];
} else if (cur[j] < secondCur) secondCur = cur[j];
}
dp = cur;
first = firstCur;
second = secondCur;
}
return Arrays.stream(dp).min().getAsInt();
}
}
Python:
class Solution:
def minCostII(self, costs: List[List[int]]) -> int:
dp, m, n = costs[0], len(costs), len(costs[0])
for i in range(1, m):
dp = [costs[i][j] + min(dp[k] for k in range(n) if k != j) for j in range(n)]
return min(dp)