Skip to content

Commit 529f822

Browse files
add 2192
1 parent 69f96b1 commit 529f822

File tree

3 files changed

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

3 files changed

+96
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
| 2200 | [Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2200.java) || Easy ||
120120
| 2196 | [Create Binary Tree From Descriptions](https://leetcode.com/problems/create-binary-tree-from-descriptions/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2196.java) || Medium | HashTable, Tree
121121
| 2194 | [Cells in a Range on an Excel Sheet](https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2194.java) || Easy ||
122+
| 2192 | [All Ancestors of a Node in a Directed Acyclic Graph](https://leetcode.com/problems/all-ancestors-of-a-node-in-a-directed-acyclic-graph/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2192.java) || Medium | Topological Sort, Graph
122123
| 2190 | [Most Frequent Number Following Key In an Array](https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2190.java) || Easy ||
123124
| 2186 | [Minimum Number of Steps to Make Two Strings Anagram II](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2186.java) || Medium ||
124125
| 2185 | [Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2185.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Queue;
8+
import java.util.TreeSet;
9+
10+
public class _2192 {
11+
public static class Solution1 {
12+
/**
13+
* My completely original solution:
14+
* topological sort template comes in handy here.
15+
*/
16+
public List<List<Integer>> getAncestors(int n, int[][] edges) {
17+
List<Integer>[] adjList = new ArrayList[n];
18+
for (int i = 0; i < n; i++) {
19+
adjList[i] = new ArrayList<>();
20+
}
21+
int[] indegree = new int[n];
22+
for (int[] edge : edges) {
23+
indegree[edge[1]]++;
24+
adjList[edge[0]].add(edge[1]);
25+
}
26+
Queue<Integer> q = new LinkedList<>();
27+
for (int i = 0; i < n; i++) {
28+
if (indegree[i] == 0) {
29+
q.offer(i);
30+
}
31+
}
32+
List<TreeSet<Integer>> treeSetList = new ArrayList<>();
33+
for (int i = 0; i < n; i++) {
34+
treeSetList.add(new TreeSet<>());
35+
}
36+
while (!q.isEmpty()) {
37+
Integer curr = q.poll();
38+
for (int v : adjList[curr]) {
39+
indegree[v]--;
40+
treeSetList.get(v).add(curr);
41+
treeSetList.get(v).addAll(treeSetList.get(curr));
42+
if (indegree[v] == 0) {
43+
q.offer(v);
44+
}
45+
}
46+
}
47+
List<List<Integer>> result = new ArrayList<>();
48+
for (int i = 0; i < n; i++) {
49+
Iterator<Integer> it = treeSetList.get(i).iterator();
50+
List<Integer> list = new ArrayList<>();
51+
while (it.hasNext()) {
52+
list.add(it.next());
53+
}
54+
result.add(list);
55+
}
56+
return result;
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions.thirdthousand._2192;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class _2192Test {
13+
private static _2192.Solution1 solution1;
14+
15+
@BeforeEach
16+
public void setup() {
17+
solution1 = new _2192.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(Arrays.asList(
23+
Arrays.asList(),
24+
Arrays.asList(),
25+
Arrays.asList(),
26+
Arrays.asList(0, 1),
27+
Arrays.asList(0, 2),
28+
Arrays.asList(0, 1, 3),
29+
Arrays.asList(0, 1, 2, 3, 4),
30+
Arrays.asList(0, 1, 2, 3)
31+
), solution1.getAncestors(8,
32+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
33+
"[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]")));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)