Skip to content

Commit 19234d9

Browse files
add 1059
1 parent 529f822 commit 19234d9

File tree

3 files changed

+123
-0
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+123
-0
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@
430430
| 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1062.java) | | Medium | String, Binary Search, DP, Rolling Hash, Suffix Array, Hash Function |
431431
| 1061 | [Lexicographically Smallest Equivalent String](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1061.java) | [:tv:](https://youtu.be/HvCaMw58_94) | Medium | Union Find
432432
| 1060 | [Missing Element in Sorted Array](https://leetcode.com/problems/missing-element-in-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1060.java) | | Medium ||Binary Search
433+
| 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java) | | Medium |DFS
433434
| 1057 | [Campus Bikes](https://leetcode.com/problems/campus-bikes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1057.java) | | Medium ||Greedy, Sort
434435
| 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1056.java) | | Easy ||
435436
| 1055 | [Fixed Point](https://leetcode.com/problems/fixed-point/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1055.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1059 {
7+
public static class Solution1 {
8+
/**
9+
* Credit: https://leetcode.com/problems/all-paths-from-source-lead-to-destination/editorial/
10+
* A very powerful algorithm, three colors to DFS a tree/graph.
11+
*/
12+
enum Color {
13+
WHITE,
14+
GRAY,
15+
BLACK
16+
}
17+
18+
public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
19+
List<Integer>[] graph = buildGraph(n, edges);
20+
Color[] colors = new Color[n];
21+
for (int i = 0; i < n; i++) {
22+
colors[i] = Color.WHITE;
23+
}
24+
return leadsToDest(graph, colors, source, destination);
25+
}
26+
27+
private boolean leadsToDest(List<Integer>[] graph, Color[] colors, int node, int destination) {
28+
//if it's not WHITE, then it should be BLACK, otherwise, there's a circle
29+
if (colors[node] != Color.WHITE) {
30+
return colors[node] == Color.BLACK;
31+
}
32+
//if this is a leaf node, then it should be destination, otherwise, it's a dead end and we return false
33+
if (graph[node].size() == 0) {
34+
return node == destination;
35+
}
36+
37+
//now, we start processing this node and mark it as GRAY
38+
colors[node] = Color.GRAY;
39+
for (int neighbor : graph[node]) {
40+
if (!leadsToDest(graph, colors, neighbor, destination)) {
41+
return false;
42+
}
43+
}
44+
//recursive processing is done, we mark it as BLACK
45+
colors[node] = Color.BLACK;
46+
return true;
47+
}
48+
49+
private static List<Integer>[] buildGraph(int n, int[][] edges) {
50+
List<Integer>[] graph = new ArrayList[n];
51+
for (int i = 0; i < n; i++) {
52+
graph[i] = new ArrayList<>();
53+
}
54+
for (int[] edge : edges) {
55+
graph[edge[0]].add(edge[1]);
56+
}
57+
return graph;
58+
}
59+
}
60+
}

Diff for: src/test/java/com/fishercoder/secondthousand/_1059Test.java

+62
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)