-
Notifications
You must be signed in to change notification settings - Fork 8
/
2392.Build a Matrix With Conditions.java
68 lines (63 loc) · 2.52 KB
/
2392.Build a Matrix With Conditions.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
65
66
67
class Solution {
public int[][] buildMatrix(int k, int[][] rowConditions, int[][] colConditions) {
Map<Integer, List<Integer>> rowGraph = buildGraph(rowConditions, k);
Map<Integer, List<Integer>> colGraph = buildGraph(colConditions, k);
List<Integer> rowOrder = topologicalSort(rowGraph);
List<Integer> colOrder = topologicalSort(colGraph);
if (rowOrder == null || colOrder == null) {
return new int[0][0];
}
int[][] positions = findPositions(rowOrder, colOrder);
int[][] result = new int[k][k];
for (int i = 0; i < k; i++) {
result[positions[i][0]][positions[i][1]] = i + 1;
}
return result;
}
private int[][] findPositions(List<Integer> rowOrder, List<Integer> colOrder) {
final int k = rowOrder.size() - 1;
int[][] result = new int[k][2];
for (int i = 0; i <= k; i++) {
if (rowOrder.get(i) != 0) result[rowOrder.get(i) - 1][0] = k - 1 - i;
}
for (int i = 0; i <= k; i++) {
if (colOrder.get(i) != 0) result[colOrder.get(i) - 1][1] = k - 1 - i;
}
return result;
}
private Map<Integer, List<Integer>> buildGraph(int[][] conditions, int k) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] condition : conditions) {
graph.putIfAbsent(condition[0], new LinkedList<Integer>());
graph.get(condition[0]).add(condition[1]);
}
graph.put(0, IntStream.range(1, k + 1).boxed().collect(Collectors.toList()));
return graph;
}
private List<Integer> topologicalSort(Map<Integer, List<Integer>> graph) {
List<Integer> result = new ArrayList<>(graph.get(0).size());
Set<Integer> visiting = new HashSet<>();
Set<Integer> visited = new HashSet<>();
if (dfs(graph, 0, visiting, visited, result)) {
return result;
}
return null;
}
private boolean dfs(Map<Integer, List<Integer>> graph, Integer cur, Set<Integer> visiting, Set<Integer> visited, List<Integer> collector) {
if (visited.contains(cur)) {
return true;
}
if (visiting.contains(cur)) {
return false;
}
visiting.add(cur);
for (Integer nxt : graph.getOrDefault(cur, Collections.emptyList())) {
if (!dfs(graph, nxt, visiting, visited, collector)) {
return false;
}
}
collector.add(cur);
visited.add(cur);
return true;
}
}