kniost

谁怕,一蓑烟雨任平生

0%

LeetCode 256/265 Paint House系列问题

题目原文

256. Paint House

Difficulty: Easy

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 _3_ cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on… Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Example:

Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue. Minimum cost: 2 + 5 + 3 = 10.

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int minCost(int[][] costs) {
if (costs == null || costs.length == 0) {
return 0;
}
int m = costs.length;
for (int i = 1; i < m; i++) {
costs[i][0] += Math.min(costs[i - 1][1], costs[i - 1][2]);
costs[i][1] += Math.min(costs[i - 1][0], costs[i - 1][2]);
costs[i][2] += Math.min(costs[i - 1][0], costs[i - 1][1]);
}
return Math.min(costs[m - 1][0], Math.min(costs[m - 1][1], costs[m - 1][2]));
}
}

题目原文

265. Paint House II

Difficulty: Hard

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.

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.

Follow up:
Could you solve it in O(nk) runtime?

解法

Language: Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public int minCostII(int[][] costs) {
if (costs == null || costs.length == 0) {
return 0;
}
int m = costs.length;
int k = costs[0].length;
// 上一列的最小值和次小值,当前列的最小值和次小值
int[] min = {0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE};
// 上一列最小值坐标,当前列最小值坐标
int[] minIndex = {-1, -1};
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
if (j == minIndex[0]) {
costs[i][j] += min[1];
} else {
costs[i][j] += min[0];
}
if (costs[i][j] <= min[2]) {
min[3] = min[2];
min[2] = costs[i][j];
minIndex[1] = j;
} else if (costs[i][j] < min[3]) {
min[3] = costs[i][j];
}
}
min[0] = min[2];
min[1] = min[3];
min[2] = Integer.MAX_VALUE;
min[3] = Integer.MAX_VALUE;
minIndex[0] = minIndex[1];
}
return min[0];
}
}