Comment on page

# 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 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 is the cost of painting house 0 with color 0; costs 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.
Could you solve it in O(nk) runtime?

## 代码

### Approach #1 DFS + Memoization

Time complexity : O(nk^2) && Space complexity : O(nk)
class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int n = costs.length;
int k = costs.length;
int[][] memo = new int[n][k];
int minCost = Integer.MAX_VALUE;
for (int color = 0; color < k; color++) {
minCost = Math.min(minCost, dfs(costs, memo, 0, color));
}
return minCost;
}
private int dfs(int[][] costs, int[][] memo, int houseNumber, int color) {
// 1. end
if (houseNumber == costs.length - 1) {
return costs[houseNumber][color];
}
if (memo[houseNumber][color] > 0) {
return memo[houseNumber][color];
}
int minCost = Integer.MAX_VALUE;
for (int nextColor = 0; nextColor < costs.length; nextColor++) {
if (color == nextColor) continue;
int curCost = dfs(costs, memo, houseNumber + 1, nextColor);
minCost = Math.min(curCost, minCost);
}
int totalCost = costs[houseNumber][color] + minCost;
memo[houseNumber][color] = totalCost;
}
}

### Approach #2 Dynamic Programming

Time complexity : `O(nk^2)` && Space complexity : `O(1)`
class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int n = costs.length;
int k = costs.length;
for (int house = 1; house < n; house++) {
for (int color = 0; color < k; color++) {
int min = Integer.MAX_VALUE;
for (int prevColor = 0; prevColor < k; prevColor++) {
if (color == prevColor) continue;
min = Math.min(min, costs[house - 1][prevColor]);
}
costs[house][color] += min;
}
}
int minCost = Integer.MAX_VALUE;
for (int cost : costs[n - 1]) {
minCost = Math.min(minCost, cost);
}
return minCost;
}
}

### Approach #3 Dynamic Programming with Optimized Time

Time complexity : `O(nk)` && Space complexity : `O(1)`
class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int k = costs.length;
int n = costs.length;
for (int house = 1; house < n; house++) {
int minColor = -1;
int secondMinColor = -1;
for (int color = 0; color < k; color++) {
int cost = costs[house - 1][color];
if (minColor == -1 || cost < costs[house - 1][minColor]) {
secondMinColor = minColor;
minColor = color;
} else if (secondMinColor == -1 || cost < costs[house - 1][secondMinColor]) {
secondMinColor = color;
}
}
for (int color = 0; color < k; color++) {
if (color == minColor) { // 不能相邻
costs[house][color] += costs[house - 1][secondMinColor];
} else {
costs[house][color] += costs[house - 1][minColor];
}
}
}
int minCost = Integer.MAX_VALUE;
for (int cost: costs[n - 1]) {
minCost = Math.min(minCost, cost);
}
return minCost;
}
}