Skip to content

Commit

Permalink
Solution to today's problem
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Jun 29, 2024
1 parent fc89216 commit 29a0534
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
45 changes: 18 additions & 27 deletions src/main/java/io/github/spannm/leetcode/lc2/lc2100/Problem2192.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,38 @@
import io.github.spannm.leetcode.LeetcodeProblem;

import java.util.*;
import java.util.stream.IntStream;

/**
* 2192. All Ancestors of a Node in a Directed Acyclic Graph.
*/
class Problem2192 extends LeetcodeProblem {

private int n;
private List<Integer>[] g;
private List<List<Integer>> ans;

public List<List<Integer>> getAncestors(int _n, int[][] _edges) {
List<List<Integer>> getAncestors(int _n, int[][] _edges) {
@SuppressWarnings("unchecked")
List<Integer>[] lists = new List[_n];
g = lists;
n = _n;
Arrays.setAll(g, i -> new ArrayList<>());
for (var e : _edges) {
g[e[0]].add(e[1]);
}
ans = new ArrayList<>();
for (int i = 0; i < _n; i++) {
ans.add(new ArrayList<>());
}
for (int i = 0; i < _n; i++) {
bfs(i);
List<Integer>[] graph = new List[_n];
Arrays.setAll(graph, i -> new ArrayList<>());
for (int[] e : _edges) {
graph[e[0]].add(e[1]);
}
return ans;
List<List<Integer>> result = new ArrayList<>();
IntStream.range(0, _n).forEach(i -> result.add(new ArrayList<>()));
IntStream.range(0, _n).forEach(i -> bfs(i, graph, result));
return result;
}

private void bfs(int s) {
static void bfs(int _s, List<Integer>[] _graph, List<List<Integer>> _result) {
Deque<Integer> q = new ArrayDeque<>();
q.offer(s);
boolean[] vis = new boolean[n];
vis[s] = true;
q.offer(_s);
boolean[] visited = new boolean[_result.size()];
visited[_s] = true;
while (!q.isEmpty()) {
int i = q.poll();
for (int j : g[i]) {
if (!vis[j]) {
vis[j] = true;
for (int j : _graph[i]) {
if (!visited[j]) {
visited[j] = true;
q.offer(j);
ans.get(j).add(s);
_result.get(j).add(_s);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.spannm.leetcode.lc2.lc2100;

import io.github.spannm.leetcode.LeetcodeBaseTest;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;

class Problem2192Test extends LeetcodeBaseTest {
@ParameterizedTest(name = "[{index}] {0}; {1} --> {2}")
@CsvSource(delimiter = ';', value = {
"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]",
"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]"
})
void test(
int _n,
@ConvertWith(CsvToIntMatrix.class) int[][] _edges,
@ConvertWith(CsvToListOfIntegerLists.class) List<List<Integer>> _expectedResult) {
assertEquals(_expectedResult, new Problem2192().getAncestors(_n, _edges));
}
}

0 comments on commit 29a0534

Please sign in to comment.