Skip to content

Commit 398cdea

Browse files
committedJul 27, 2024
solves #797: All Paths From Source to Target in java
1 parent c06e63f commit 398cdea

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
 

Diff for: ‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@
414414
| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes) | [![Java](assets/java.png)](src/MinimumAbsoluteDifferenceInBST.java) [![Python](assets/python.png)](python/minimum_distance_between_bst_nodes.py) | |
415415
| 788 | [Rotated Digits](https://leetcode.com/problems/rotated-digits) | | |
416416
| 796 | [Rotate String](https://leetcode.com/problems/rotate-string) | [![Java](assets/java.png)](src/RotateString.java) | |
417+
| 797 | [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target) | [![Java](assets/java.png)](src/AllPathsFromSourceToTarget.java) | |
417418
| 800 | [Similar RGB Color](https://leetcode.com/problems/similar-rgb-color) | | |
418419
| 804 | [Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words) | [![Java](assets/java.png)](src/UniqueMorseCodeWords.java) | |
419420
| 806 | [Number of Lines to Write String](https://leetcode.com/problems/number-of-lines-to-write-string) | [![Java](assets/java.png)](src/NumberOfLinesToWriteInString.java) | |

Diff for: ‎src/AllPathsFromSourceToTarget.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/all-paths-from-source-to-target
2+
// maximum edges for DAG = V-1, possible paths = 2^(V-1), for every path we clone path O(V)
3+
// T: O(2^V * V)
4+
// S: O(V)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class AllPathsFromSourceToTarget {
10+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
11+
final List<List<Integer>> result = new ArrayList<>();
12+
final int n = graph.length;
13+
dfs(graph, 0, n - 1, new ArrayList<>() {{ add(0); }}, result);
14+
return result;
15+
}
16+
17+
private static void dfs(int[][] graph, int current, int target, List<Integer> path, List<List<Integer>> result) {
18+
if (current == target) {
19+
result.add(new ArrayList<>(path));
20+
return;
21+
}
22+
for (int edge : graph[current]) {
23+
path.add(edge);
24+
dfs(graph, edge, target, path, result);
25+
path.removeLast();
26+
}
27+
}
28+
}

Diff for: ‎src/HelloWorld.java

+49
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1+
// T: O(N + E alp(N))
2+
// S: O(N)
3+
14
public class HelloWorld {
5+
private static final class DisjointSet {
6+
private final int[] roots, rank;
7+
8+
public DisjointSet(int size) {
9+
roots = new int[size];
10+
rank = new int[size];
11+
for (int i = 0 ; i < size ; i++) {
12+
roots[i] = i;
13+
rank[i] = 1;
14+
}
15+
}
16+
17+
public int find(int num) {
18+
if (num == roots[num]) {
19+
return num;
20+
}
21+
return roots[num] = find(roots[num]);
22+
}
23+
24+
public boolean areConnected(int x, int y) {
25+
return find(x) == find(y);
26+
}
27+
28+
public void union(int x, int y) {
29+
final int rootX = find(x), rootY = find(y);
30+
if (rootX == rootY) {
31+
return;
32+
}
33+
if (rank[rootX] > rank[rootY]) {
34+
roots[rootY] = rootX;
35+
} else if (rank[rootX] < rank[rootY]) {
36+
roots[rootX] = rootY;
37+
} else {
38+
roots[rootY] = rootX;
39+
rank[rootX]++;
40+
}
41+
}
42+
}
43+
44+
public boolean validPath(int n, int[][] edges, int source, int destination) {
45+
final DisjointSet disjointSet = new DisjointSet(n);
46+
for (int[] edge : edges) {
47+
disjointSet.union(edge[0], edge[1]);
48+
}
49+
return disjointSet.areConnected(source, destination);
50+
}
251
}

0 commit comments

Comments
 (0)
Please sign in to comment.