forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC265_PaintHouseII.java
More file actions
40 lines (39 loc) · 1.41 KB
/
LC265_PaintHouseII.java
File metadata and controls
40 lines (39 loc) · 1.41 KB
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
36
37
38
39
40
import java.util.*;
public class LC265_PaintHouseII {
public int minCostII(int[][] costs) {
if(costs == null || costs.length == 0 || costs[0].length == 0) return 0;
int row = costs[0].length, col = costs.length;
if(row == 1) return costs[0][0]; // in case there is only one color.
int[][] minTwo = new int[2][2]; // first value, second index.
Comparator<int[]> cmp = new Comparator<int[]>(){
public int compare(int[] pair1, int[] pair2){
return pair1[0] - pair2[0];
}
};
Queue<int[]> pq = new PriorityQueue<>(row, cmp);
for(int i = 0; i < row; ++i){
pq.add(new int[]{costs[0][i], i});
}
int cnt = 0;
while(cnt < 2){
minTwo[cnt++] = pq.poll();
}
System.out.println(minTwo[0][0] + " " + minTwo[1][0]);
for(int j = 1; j < col; ++j){
pq = new PriorityQueue<>(row, cmp);
for(int i = 0; i < row; ++i){
if(i == minTwo[0][1]){ // equals min index
pq.add(new int[]{minTwo[1][0] + costs[j][i], i});
}else{
pq.add(new int[]{minTwo[0][0] + costs[j][i], i});
}
}
cnt = 0;
while(cnt < 2){ // get the minimum two values
minTwo[cnt++] = pq.poll();
}
System.out.println(minTwo[0][0] + " " + minTwo[1][0]);
}
return minTwo[0][0];
}
}