-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1605.java
42 lines (39 loc) · 1.95 KB
/
_1605.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
36
37
38
39
40
41
42
package com.fishercoder.solutions.secondthousand;
import java.util.PriorityQueue;
public class _1605 {
public static class Solution1 {
/*
* My completely original solution:
* 1. sort out your logic with a pen and paper first, greedy algorithm should be the way to go;
* 2. each time, take out the minimum value from both rowSet and colSet, put that entire value onto the result grid,
* then deduct that value from the other set if they are not equal, put it back into the minHeap, repeat until both minHeaps are empty;
*/
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
// form two minHeaps, use their values to sort
PriorityQueue<int[]> rowMinHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
for (int i = 0; i < rowSum.length; i++) {
rowMinHeap.offer(new int[] {i, rowSum[i]});
}
PriorityQueue<int[]> colMinHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
for (int j = 0; j < colSum.length; j++) {
colMinHeap.offer(new int[] {j, colSum[j]});
}
int[][] result = new int[rowSum.length][colSum.length];
while (!colMinHeap.isEmpty() && !rowMinHeap.isEmpty()) {
int[] minRow = rowMinHeap.poll();
int[] minCol = colMinHeap.poll();
if (minRow[1] < minCol[1]) {
result[minRow[0]][minCol[0]] = minRow[1];
colMinHeap.offer(new int[] {minCol[0], minCol[1] - minRow[1]});
} else if (minRow[1] > minCol[1]) {
result[minRow[0]][minCol[0]] = minCol[1];
rowMinHeap.offer(new int[] {minRow[0], minRow[1] - minCol[1]});
} else {
// the min values from row and col are equal
result[minRow[0]][minCol[0]] = minCol[1];
}
}
return result;
}
}
}