Skip to content

Commit f3e4729

Browse files
refactor 265
1 parent 3cd42b9 commit f3e4729

File tree

1 file changed

+39
-59
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+39
-59
lines changed
Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,45 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 265. Paint House II
5-
*
6-
* There are a row of n houses, each house can be painted with one of the k colors.
7-
* The cost of painting each house with a certain color is different.
8-
* You have to paint all the houses such that no two adjacent houses have the same color.
9-
* The cost of painting each house with a certain color is represented by a n x k cost matrix.
10-
*
11-
* For example, costs[0][0] is the cost of painting house 0 with color 0;
12-
* costs[1][2] is the cost of painting house 1 with color 2,
13-
* and so on...
14-
*
15-
* Find the minimum cost to paint all houses.
16-
17-
Note:
18-
All costs are positive integers.
19-
20-
Follow up:
21-
Could you solve it in O(nk) runtime?
22-
*/
233
public class _265 {
244

25-
public static class Solution1 {
26-
public int minCostII(int[][] costs) {
27-
if (costs == null || costs.length == 0) {
28-
return 0;
29-
}
30-
31-
int n = costs.length;
32-
int k = costs[0].length;
33-
// min1 is the index of the 1st-smallest cost till previous house
34-
// min2 is the index of the 2nd-smallest cost till previous house
35-
int min1 = -1;
36-
int min2 = -1;
37-
38-
for (int i = 0; i < n; i++) {
39-
int last1 = min1;
40-
int last2 = min2;
41-
min1 = -1;
42-
min2 = -1;
43-
44-
for (int j = 0; j < k; j++) {
45-
if (j != last1) {
46-
// current color j is different to last min1
47-
costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1];
48-
} else {
49-
costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2];
50-
}
51-
52-
// find the indices of 1st and 2nd smallest cost of painting current house i
53-
if (min1 < 0 || costs[i][j] < costs[i][min1]) {
54-
min2 = min1;
55-
min1 = j;
56-
} else if (min2 < 0 || costs[i][j] < costs[i][min2]) {
57-
min2 = j;
58-
}
59-
}
60-
}
61-
return costs[n - 1][min1];
62-
}
63-
}
5+
public static class Solution1 {
6+
public int minCostII(int[][] costs) {
7+
if (costs == null || costs.length == 0) {
8+
return 0;
9+
}
10+
11+
int n = costs.length;
12+
int k = costs[0].length;
13+
// min1 is the index of the 1st-smallest cost till previous house
14+
// min2 is the index of the 2nd-smallest cost till previous house
15+
int min1 = -1;
16+
int min2 = -1;
17+
18+
for (int i = 0; i < n; i++) {
19+
int last1 = min1;
20+
int last2 = min2;
21+
min1 = -1;
22+
min2 = -1;
23+
24+
for (int j = 0; j < k; j++) {
25+
if (j != last1) {
26+
// current color j is different to last min1
27+
costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1];
28+
} else {
29+
costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2];
30+
}
31+
32+
// find the indices of 1st and 2nd smallest cost of painting current house i
33+
if (min1 < 0 || costs[i][j] < costs[i][min1]) {
34+
min2 = min1;
35+
min1 = j;
36+
} else if (min2 < 0 || costs[i][j] < costs[i][min2]) {
37+
min2 = j;
38+
}
39+
}
40+
}
41+
return costs[n - 1][min1];
42+
}
43+
}
6444

6545
}

0 commit comments

Comments
 (0)