Skip to content

Commit 29a0534

Browse files
committed
Solution to today's problem
1 parent fc89216 commit 29a0534

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

src/main/java/io/github/spannm/leetcode/lc2/lc2100/Problem2192.java

+18-27
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,38 @@
33
import io.github.spannm.leetcode.LeetcodeProblem;
44

55
import java.util.*;
6+
import java.util.stream.IntStream;
67

78
/**
89
* 2192. All Ancestors of a Node in a Directed Acyclic Graph.
910
*/
1011
class Problem2192 extends LeetcodeProblem {
1112

12-
private int n;
13-
private List<Integer>[] g;
14-
private List<List<Integer>> ans;
15-
16-
public List<List<Integer>> getAncestors(int _n, int[][] _edges) {
13+
List<List<Integer>> getAncestors(int _n, int[][] _edges) {
1714
@SuppressWarnings("unchecked")
18-
List<Integer>[] lists = new List[_n];
19-
g = lists;
20-
n = _n;
21-
Arrays.setAll(g, i -> new ArrayList<>());
22-
for (var e : _edges) {
23-
g[e[0]].add(e[1]);
24-
}
25-
ans = new ArrayList<>();
26-
for (int i = 0; i < _n; i++) {
27-
ans.add(new ArrayList<>());
28-
}
29-
for (int i = 0; i < _n; i++) {
30-
bfs(i);
15+
List<Integer>[] graph = new List[_n];
16+
Arrays.setAll(graph, i -> new ArrayList<>());
17+
for (int[] e : _edges) {
18+
graph[e[0]].add(e[1]);
3119
}
32-
return ans;
20+
List<List<Integer>> result = new ArrayList<>();
21+
IntStream.range(0, _n).forEach(i -> result.add(new ArrayList<>()));
22+
IntStream.range(0, _n).forEach(i -> bfs(i, graph, result));
23+
return result;
3324
}
3425

35-
private void bfs(int s) {
26+
static void bfs(int _s, List<Integer>[] _graph, List<List<Integer>> _result) {
3627
Deque<Integer> q = new ArrayDeque<>();
37-
q.offer(s);
38-
boolean[] vis = new boolean[n];
39-
vis[s] = true;
28+
q.offer(_s);
29+
boolean[] visited = new boolean[_result.size()];
30+
visited[_s] = true;
4031
while (!q.isEmpty()) {
4132
int i = q.poll();
42-
for (int j : g[i]) {
43-
if (!vis[j]) {
44-
vis[j] = true;
33+
for (int j : _graph[i]) {
34+
if (!visited[j]) {
35+
visited[j] = true;
4536
q.offer(j);
46-
ans.get(j).add(s);
37+
_result.get(j).add(_s);
4738
}
4839
}
4940
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.spannm.leetcode.lc2.lc2100;
2+
3+
import io.github.spannm.leetcode.LeetcodeBaseTest;
4+
import java.util.List;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.converter.ConvertWith;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
9+
class Problem2192Test extends LeetcodeBaseTest {
10+
@ParameterizedTest(name = "[{index}] {0}; {1} --> {2}")
11+
@CsvSource(delimiter = ';', value = {
12+
"8; [0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]; [],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]",
13+
"5; [0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]; [],[0],[0,1],[0,1,2],[0,1,2,3]"
14+
})
15+
void test(
16+
int _n,
17+
@ConvertWith(CsvToIntMatrix.class) int[][] _edges,
18+
@ConvertWith(CsvToListOfIntegerLists.class) List<List<Integer>> _expectedResult) {
19+
assertEquals(_expectedResult, new Problem2192().getAncestors(_n, _edges));
20+
}
21+
}

0 commit comments

Comments
 (0)