-
Notifications
You must be signed in to change notification settings - Fork 0
/
2392.java
64 lines (54 loc) · 2.07 KB
/
2392.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// https://leetcode.com/problems/build-a-matrix-with-conditions/?envType=daily-question&envId=2024-07-21
import java.util.*;
class Solution {
public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {
List<Integer> toporow = topoSort(rowConditions, k);
List<Integer> topocol = topoSort(colConditions, k);
// guhhhhh
if (toporow.isEmpty() || topocol.isEmpty()) return new int[0][0];
int[][] matrix = new int[k][k];
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
if (toporow.get(i).equals(topocol.get(j))) {
matrix[i][j] = toporow.get(i);
}
}
}
return matrix;
}
private List<Integer> topoSort(int[][] edges, int n) {
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i <= n; i++) {
adj.add(new ArrayList<Integer>());
}
for (int[] edge : edges) {
adj.get(edge[0]).add(edge[1]);
}
List<Integer> topo = new ArrayList<>();
int[] vis = new int[n+1]; // 0 = nonvis, 1 = currently on, 2 = visited alr
boolean[] cyclic = {false}; // if I make this a var it gets passed by value
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
dfs(i, adj, vis, topo, cyclic);
if (cyclic[0]) return new ArrayList<>();
}
}
Collections.reverse(topo);
return topo;
}
private void dfs(int cur, List<List<Integer>> adj, int[] vis, List<Integer> topo, boolean[] cyclic) {
vis[cur] = 1;
for (int dest : adj.get(cur)) {
if (vis[dest] == 0) {
dfs(dest, adj, vis, topo, cyclic);
if (cyclic[0]) return; // i hate that cycles can be found
} else if (vis[dest] == 1) {
// how did we just get to a node we are visiting???
cyclic[0] = true;
return;
}
}
vis[cur] = 2; // ok now we're done
topo.add(cur);
}
}