您的位置:首页 > 大数据 > 人工智能

LeetCode 265. Paint House II(房子涂色)

2016-04-10 16:22 591 查看
原题网址:https://leetcode.com/problems/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[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?

方法:动态规划,求出最低成本和次低成本,如果房子的颜色和上一个房子最低成本的颜色相同,则涂上次低成本的颜色,否则涂最低成本的颜色。

这道题不需要我们分别求出与每一个颜色不同的最低成本值,只需要求出最低和次低,就可以有效避免颜色间隔的问题,这是关键。

public class Solution {
private void find(int[] cost, int[] min, int[] color) {
color[0] = -1;
color[1] = -1;
for(int i=0; i<cost.length; i++) {
if (color[0] == -1 || cost[i] < min[0]) {
min[0] = cost[i];
color[0] = i;
}
}
for(int i=0; i<cost.length; i++) {
if (i != color[0] && (color[1] == -1 || cost[i] < min[1])) {
min[1] = cost[i];
color[1] = i;
}
}
}
public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int[] minCost = new int[2];
int[] color = new int[2];
int[] nextMinCost = new int[2];
int[] nextColor = new int[2];
find(costs[0], minCost, color);
for(int i=1; i<costs.length; i++) {
for(int j=0; j<costs[i].length; j++) {
costs[i][j] += j!=color[0] ? minCost[0] : minCost[1];
}
find(costs[i], nextMinCost, nextColor);
int[] t1 = minCost;
minCost = nextMinCost;
nextMinCost = t1;
int[] t2 = color;
color = nextColor;
nextColor = t2;
}
return minCost[0];
}
}


可以进一步优化得更简洁:

public class Solution {
// 求出最低成本和次低成本
private void mins(int[] cost, int[] min) {
for(int i=0; i<cost.length; i++) {
if (i==0 || cost[i] < cost[min[0]]) min[0] = i;
}
if (min[0] == 0) min[1] = 1; else min[1] = 0;
for(int i=0; i<cost.length; i++) {
if (i != min[0] && cost[i] < cost[min[1]]) min[1] = i;
}
}

public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int[] minCost = new int[costs[0].length];
int[] min = new int[2];
for(int c=0; c<costs[0].length; c++) {
minCost[c] = costs[0][c];
}
mins(minCost, min);
for(int h=1; h<costs.length; h++) {
int[] prevCost = minCost;
minCost = new int[costs[0].length];
for(int c=0; c<costs[h].length; c++) {
if (c != min[0]) minCost[c] = prevCost[min[0]] + costs[h][c];
else minCost[c] = prevCost[min[1]] + costs[h][c];
}
mins(minCost, min);
}
return minCost[min[0]];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: