标签:
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. Follow up: Could you solve it in O(nk) runtime?
Time Complexity: O(n*k*k)
1 public class Solution { 2 public int minCostII(int[][] costs) { 3 if (costs==null || costs.length==0 || costs[0].length==0) return 0; 4 int n = costs.length; 5 int k = costs[0].length; 6 int[][] dp = new int[n][k]; 7 for (int j=0; j<k; j++) { 8 dp[0][j] = costs[0][j]; 9 } 10 11 for (int i=1; i<n; i++) { 12 for (int j=0; j<k; j++) { 13 int min = Integer.MAX_VALUE; 14 for (int t=0; t<k; t++) { 15 min = (t==j)? min : Math.min(min, dp[i-1][t]); 16 } 17 dp[i][j] = min + costs[i][j]; 18 } 19 } 20 21 int res = Integer.MAX_VALUE; 22 for (int j=0; j<k; j++) { 23 res = Math.min(res, dp[n-1][j]); 24 } 25 return res; 26 } 27 }
Time Complexity O(n*k) 的方法参考:http://buttercola.blogspot.com/2015/09/leetcode-paint-house-ii.html(未深究)
1 public class Solution { 2 public int minCostII(int[][] costs) { 3 if (costs == null || costs.length == 0) { 4 return 0; 5 } 6 7 int n = costs.length; 8 int k = costs[0].length; 9 10 // dp[j] means the min cost for color j 11 int[] dp = new int[k]; 12 int min1 = 0; 13 int min2 = 0; 14 15 for (int i = 0; i < n; i++) { 16 int oldMin1 = min1; 17 int oldMin2 = min2; 18 19 min1 = Integer.MAX_VALUE; 20 min2 = Integer.MAX_VALUE; 21 22 for (int j = 0; j < k; j++) { 23 if (dp[j] != oldMin1 || oldMin1 == oldMin2) { 24 dp[j] = oldMin1 + costs[i][j]; 25 } else { 26 dp[j] = oldMin2 + costs[i][j]; 27 } 28 29 if (min1 <= dp[j]) { 30 min2 = Math.min(min2, dp[j]); 31 } else { 32 min2 = min1; 33 min1 = dp[j]; 34 } 35 } 36 37 } 38 39 return min1; 40 } 41 }
还有Space O(k) 1-D DP方法:(未深究)
1 public class Solution { 2 public int minCostII(int[][] costs) { 3 if (costs == null || costs.length == 0) { 4 return 0; 5 } 6 7 int n = costs.length; 8 int k = costs[0].length; 9 10 // dp[j] means the min cost for color j 11 int[] dp1 = new int[k]; 12 int[] dp2 = new int[k]; 13 14 // Initialization 15 for (int i = 0; i < k; i++) { 16 dp1[i] = costs[0][i]; 17 } 18 19 for (int i = 1; i < n; i++) { 20 for (int j = 0; j < k; j++) { 21 dp2[j] = Integer.MAX_VALUE; 22 for (int m = 0; m < k; m++) { 23 if (m != j) { 24 dp2[j] = Math.min(dp1[m] + costs[i][j], dp2[j]); 25 } 26 } 27 } 28 29 for (int j = 0; j < k; j++) { 30 dp1[j] = dp2[j]; 31 } 32 } 33 34 // Final state 35 int minCost = Integer.MAX_VALUE; 36 for (int i = 0; i < k; i++) { 37 minCost = Math.min(minCost, dp1[i]); 38 } 39 40 return minCost; 41 } 42 }
标签:
原文地址:http://www.cnblogs.com/EdwardLiu/p/5069692.html