Question

Formatted question description: https://leetcode.ca/all/265.html

 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,
costs0 is the cost of painting house 0 with color 0;
costs1 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:

input[0] => house-0
input[1] => house-1

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?



Algorithm

3 for loops

Use dynamic programming. Create a 2D array dp with n rows and k columns. The element dp[i][j] represents the minimum total cost so far to paint houses 0 to i with the house at index i painted with color j.

For row 0 which corresponds to house 0, dp[0][j] equals costs[0][j]. To get dp[i][j] for i > 0, first find the minimum in row dp[i - 1] except dp[i - 1][j], which is represented as min here, since any two adjacent houses must be painted with different colors, then set dp[i][j] = min + costs[i][j].

Finally, find the minimum element from row dp[n - 1] and return the minimum value, which is the minimum cost to paint all houses.

Follow up: tracking min1 and min2

Finding the minimum value of different colors is not to traverse all the different colors, but to use min1 and min2 to record the smallest and second smallest cost colors

• If the current house color is the same as min1, use the value corresponding to min2 to calculate
• On the contrary, use the value corresponding to min1. This solution actually includes the method of finding the sub-smallest value

Code

Java

• public class Paint_House_II {

// O(n * k^2)
class Solution_N3 {
public int minCostII(int[][] costs) {
if (costs.length == 0 || costs[0].length == 0)
return 0;
int length = costs.length, colors = costs[0].length;
int[][] dp = new int[length][colors];
for (int i = 0; i < colors; i++)
dp[0][i] = costs[0][i];
for (int i = 1; i < length; i++) {
for (int j = 0; j < colors; j++) {
int curCost = costs[i][j];
int prevMinSum = Integer.MAX_VALUE;
for (int k = 0; k < colors; k++) {
if (j == k)
continue;
prevMinSum = Math.min(prevMinSum, dp[i - 1][k]);
}
dp[i][j] = prevMinSum + curCost;
}
}
int minCost = Integer.MAX_VALUE;
for (int i = 0; i < colors; i++)
minCost = Math.min(minCost, dp[length - 1][i]);
return minCost;
}
}

public class Solution {
public int minCostII(int[][] costs) {

if(costs != null && costs.length == 0) {
return 0;
}

int prevMin = 0, prevSecondMin = 0, prevIdx = -1;

for(int i = 0; i < costs.length; i++){

int currMin = Integer.MAX_VALUE;
int currSecondMin = Integer.MAX_VALUE;
int currIdx = -1;

for(int j = 0; j < costs[0].length; j++){ // go over all colors

costs[i][j] = costs[i][j] + (prevIdx == j ? prevSecondMin : prevMin);

if(costs[i][j] < currMin){
currSecondMin = currMin;
currMin = costs[i][j];
currIdx = j;
} else if (costs[i][j] < currSecondMin){
currSecondMin = costs[i][j];
}
}
prevMin = currMin;
prevSecondMin = currSecondMin;
prevIdx = currIdx;
}
return prevMin;
}
}

public class Solution_DP_O_nk {
public int minCostII(int[][] costs) {

if(costs != null && costs.length == 0) {
return 0;
}

int[][] dp = costs;
int min1 = -1, min2 = -1;

for (int i = 0; i < dp.length; i++) {
int last1 = min1;
int last2 = min2;
min1 = -1; min2 = -1;
for (int j = 0; j < dp[i].length; j++) {
if (j != last1) {
dp[i][j] += last1 < 0 ? 0 : dp[i - 1][last1];
} else {
dp[i][j] += last2 < 0 ? 0 : dp[i - 1][last2];
}

if (min1 < 0 || dp[i][j] < dp[i][min1]) {
min2 = min1;
min1 = j;
} else if (min2 < 0 || dp[i][j] < dp[i][min2]) {
min2 = j;
}
}
}

return dp[dp.length - 1][min1];
}
}
}

• Todo

• class Solution(object):
def minCostII(self, costs):
"""
:type costs: List[List[int]]
:rtype: int
"""
if not costs:
return 0
dp = [[0] * len(costs[0]) for _ in range(0, len(costs))]
dp[0] = costs[0]
for i in range(1, len(costs)):
for j in range(0, len(costs[0])):
dp[i][j] = min(dp[i - 1][:j] + dp[i - 1][j + 1:]) + costs[i][j] # exclude j itself
return min(dp[-1])