题目:
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.java
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.code
Note:
All costs are positive integers.leetcode
Follow up:
Could you solve it in O(nk) runtime?it
解答:
利用paint house的思路,只不过把三种颜色换成了k种颜色,因此:io
//State: f[i][j] is the minimum cost of painting i houses with color j //Function: f[i][j] = Math.min(f[i - 1][k(except j)]) + costs[i][j]; //Initialize: f[0][j] = costs[0][j]; //Result: Math.min(f[costs.length - 1][j]); public int minCostII(int[][] costs) { if (costs == null || costs.length == 0 || costs[0].length == 0) { return 0; } int house = costs.length, color = costs[0].length; int[][] f = new int[house][color]; for (int i = 0; i < color; i++) { f[0][i] = costs[0][i]; } for (int i = 1; i < house; i++) { for (int j = 0; j < color; j++) { f[i][j] = Integer.MAX_VALUE; for (int k = 0; k < color; k++) { if (k == j) continue; f[i][j] = Math.min(f[i][j], f[i - 1][k] + costs[i][j]); } } } int result = Integer.MAX_VALUE; for (int i = 0; i < color; i++) { result = Math.min(result, f[house - 1][i]); } return result;
followup是如何把它的复杂度降到o(nk), 那么就是若是将颜色的部分只扫一遍。参考leetcode的discuss里most voted answer, 只须要记录下每一个house的最小的两个颜色。若是下一个颜色跟这个颜色不同,就取最小的这个颜色加上此次所选的颜色,并找出最小值;若是下一个颜色跟这个颜色同样,那么咱们不能够取最小的这个颜色,因此咱们取第二小的颜色加上此次所选的颜色。最后把最小的颜色输出就能够了。ast
//Only the first two minimum costs count, so we keep track on min1 and min2 for each house public int minCostII(int[][] costs) { if (costs == null || costs.length == 0 || costs[0].length == 0) { return 0; } int house = costs.length, color = costs[0].length; int min1 = -1, min2 = -1; for (int i = 0; i < house; i++) { int last1 = min1, last2 = min2; min1 = -1; min2 = -1; for (int j = 0; j < color; j++) { if (j != last1) { costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1]; } else { costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2]; } if (min1 < 0 || costs[i][j] < costs[i][min1]) { min2 = min1; min1 = j; } else if (min2 < 0 || costs[i][j] < costs[i][min2]) { min2 = j; } } } return costs[house - 1][min1]; }