Skip to content

Commit e5cb667

Browse files
add 2392
1 parent cb84167 commit e5cb667

File tree

3 files changed

+97
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+97
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java) || Easy ||
5959
| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java) || Medium ||
6060
| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java) || Easy ||
61+
| 2392 | [Build a Matrix With Conditions](https://leetcode.com/problems/build-a-matrix-with-conditions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2392.java) || Hard | Topological Sot, Graph, Matrix
6162
| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2389.java) || Easy |
6263
| 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java) || Medium | BFS
6364
| 2380 | [Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java) || Medium ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class _2392 {
9+
public static class Solution1 {
10+
/**
11+
* I figured out I needed to use Kahn's algorithm to topologically sort both rowConditions and colConditions,
12+
* but unsure how to fill the matrix.
13+
* https://leetcode.com/problems/build-a-matrix-with-conditions/editorial/ is brilliant as of how to build the matrix:
14+
* using its slides to step through helps a lot!
15+
*/
16+
public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {
17+
int[] topologicallySortedRows = topologicalSort(rowConditions, k);
18+
int[] topologicallySortedCols = topologicalSort(colConditions, k);
19+
if (topologicallySortedRows.length == 0 || topologicallySortedCols.length == 0) {
20+
return new int[][]{};
21+
}
22+
int[][] matrix = new int[k][k];
23+
for (int i = 0; i < k; i++) {
24+
for (int j = 0; j < k; j++) {
25+
if (topologicallySortedRows[i] == topologicallySortedCols[j]) {
26+
matrix[i][j] = topologicallySortedCols[j];
27+
}
28+
}
29+
}
30+
return matrix;
31+
}
32+
33+
private int[] topologicalSort(int[][] conditions, int k) {
34+
List<Integer>[] adj = new ArrayList[k + 1];
35+
for (int i = 0; i <= k; i++) {
36+
adj[i] = new ArrayList<>();
37+
}
38+
int[] indegree = new int[k + 1];
39+
int[] order = new int[k];
40+
int index = 0;
41+
for (int[] x : conditions) {
42+
adj[x[0]].add(x[1]);
43+
indegree[x[1]]++;
44+
}
45+
Queue<Integer> q = new LinkedList<>();
46+
for (int i = 1; i <= k; i++) {
47+
if (indegree[i] == 0) {
48+
q.offer(i);
49+
}
50+
}
51+
while (!q.isEmpty()) {
52+
Integer curr = q.poll();
53+
order[index++] = curr;
54+
k--;
55+
for (int v : adj[curr]) {
56+
indegree[v]--;
57+
if (indegree[v] == 0) {
58+
q.offer(v);
59+
}
60+
}
61+
}
62+
if (k != 0) {
63+
return new int[0];
64+
}
65+
return order;
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions.thirdthousand._2392;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class _2392Test {
9+
private static _2392.Solution1 solution1;
10+
11+
@BeforeEach
12+
public void setup() {
13+
solution1 = new _2392.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
CommonUtils.print2DIntArray(solution1.buildMatrix(
19+
3,
20+
new int[][]{
21+
{1, 2}, {3, 2}
22+
},
23+
new int[][]{
24+
{2, 1}, {3, 2}
25+
}));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)