Skip to content

Commit d4763a2

Browse files
add 1605
1 parent 7a395d2 commit d4763a2

File tree

3 files changed

+68
-0
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+68
-0
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1614.java) || Easy | String |
172172
| 1609 | [Even Odd Tree](https://leetcode.com/problems/even-odd-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1609.java) || Medium | Tree |
173173
| 1608 | [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1608.java) || Easy | Array |
174+
| 1605 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1605.java) || Medium | Greedy, Array, Matrix |
174175
| 1604 | [Alert Using Same Key-Card Three or More Times in a One Hour Period](https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1604.java) || Medium | String, Ordered Map |
175176
| 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1603.java) || Easy | Design |
176177
| 1602 | [Find Nearest Right Node in Binary Tree](https://leetcode.com/problems/find-nearest-right-node-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1602.java) || Medium | Tree, BFS |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.PriorityQueue;
4+
5+
public class _1605 {
6+
public static class Solution1 {
7+
/**
8+
* My completely original solution:
9+
* 1. sort out your logic with a pen and paper first, greedy algorithm should be the way to go;
10+
* 2. each time, take out the minimum value from both rowSet and colSet, put that entire value onto the result grid,
11+
* 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;
12+
*/
13+
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
14+
//form two minHeaps, use their values to sort
15+
PriorityQueue<int[]> rowMinHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
16+
for (int i = 0; i < rowSum.length; i++) {
17+
rowMinHeap.offer(new int[]{i, rowSum[i]});
18+
}
19+
PriorityQueue<int[]> colMinHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
20+
for (int j = 0; j < colSum.length; j++) {
21+
colMinHeap.offer(new int[]{j, colSum[j]});
22+
}
23+
24+
int[][] result = new int[rowSum.length][colSum.length];
25+
while (!colMinHeap.isEmpty() && !rowMinHeap.isEmpty()) {
26+
int[] minRow = rowMinHeap.poll();
27+
int[] minCol = colMinHeap.poll();
28+
if (minRow[1] < minCol[1]) {
29+
result[minRow[0]][minCol[0]] = minRow[1];
30+
colMinHeap.offer(new int[]{minCol[0], minCol[1] - minRow[1]});
31+
} else if (minRow[1] > minCol[1]) {
32+
result[minRow[0]][minCol[0]] = minCol[1];
33+
rowMinHeap.offer(new int[]{minRow[0], minRow[1] - minCol[1]});
34+
} else {
35+
//the min values from row and col are equal
36+
result[minRow[0]][minCol[0]] = minCol[1];
37+
}
38+
}
39+
return result;
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.solutions.secondthousand._1605;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _1605Test {
10+
private static _1605.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _1605.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int[][] expected = new int[][]{
20+
{0, 5, 0}, {0, 1, 6}, {8, 0, 2}
21+
};
22+
assertArrayEquals(expected, solution1.restoreMatrix(new int[]{5, 7, 10}, new int[]{8, 6, 8}));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)