Skip to content

Commit b315c79

Browse files
add 1971
1 parent f79bbc1 commit b315c79

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ _If you like this project, please leave me a star._ ★
3535
|1980|[Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1980.java) ||Medium||
3636
|1979|[Find Greatest Common Divisor of Array](https://leetcode.com/problems/find-greatest-common-divisor-of-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1979.java) ||Easy||
3737
|1974|[Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1974.java) ||Easy||
38+
|1971|[Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1971.java) ||Easy|BFS, DFS, Graph|
3839
|1968|[Array With Elements Not Equal to Average of Neighbors](https://leetcode.com/problems/array-with-elements-not-equal-to-average-of-neighbors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1968.java) ||Medium||
3940
|1967|[Number of Strings That Appear as Substrings in Word](https://leetcode.com/problems/number-of-strings-that-appear-as-substrings-in-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1967.java) ||Easy||
4041
|1961|[Check If String Is a Prefix of Array](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1961.java) ||Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.LinkedList;
6+
import java.util.Map;
7+
import java.util.Queue;
8+
import java.util.Set;
9+
10+
public class _1971 {
11+
public static class Solution1 {
12+
public boolean validPath(int n, int[][] edges, int start, int end) {
13+
if (start == end) {
14+
return true;
15+
}
16+
Map<Integer, Set<Integer>> neighborsMap = new HashMap<>();
17+
for (int[] edge : edges) {
18+
int u = edge[0];
19+
int v = edge[1];
20+
Set<Integer> neighbors1 = neighborsMap.getOrDefault(u, new HashSet<>());
21+
neighbors1.add(v);
22+
neighborsMap.put(u, neighbors1);
23+
24+
Set<Integer> neighbors2 = neighborsMap.getOrDefault(v, new HashSet<>());
25+
neighbors2.add(u);
26+
neighborsMap.put(v, neighbors2);
27+
}
28+
Queue<Integer> queue = new LinkedList<>();
29+
Set<Integer> visitedVertices = new HashSet<>();
30+
queue.offer(start);
31+
while (!queue.isEmpty()) {
32+
int size = queue.size();
33+
for (int i = 0; i < size; i++) {
34+
Integer curr = queue.poll();
35+
for (int neighbor : neighborsMap.getOrDefault(curr, new HashSet<>())) {
36+
if (neighbor == end) {
37+
return true;
38+
}
39+
if (visitedVertices.add(neighbor)) {
40+
queue.offer(neighbor);
41+
}
42+
}
43+
}
44+
}
45+
return false;
46+
}
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1971;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1971Test {
11+
private static _1971.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1971.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
int[][] edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[1,2],[2,0]");
21+
assertEquals(true, solution1.validPath(3, edges, 0, 2));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
int[][] edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1],[0,2],[3,5],[5,4],[4,3]");
27+
assertEquals(false, solution1.validPath(6, edges, 0, 5));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
int[][] edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[4,3],[1,4],[4,8],[1,7],[6,4],[4,2],[7,4],[4,0],[0,9],[5,4]");
33+
assertEquals(true, solution1.validPath(10, edges, 5, 9));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
int[][] edges = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,7],[0,8],[6,1],[2,0],[0,4],[5,8],[4,7],[1,3],[3,5],[6,5]");
39+
assertEquals(true, solution1.validPath(10, edges, 7, 5));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
assertEquals(true, solution1.validPath(1, new int[][]{}, 0, 0));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)