Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

​

Solution: Find if Path Exists in Graph(easy)

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

The solution returns true at n = 1, but there can be no self-edges. This should return false.

Show 1 reply
A

Anonymous

· 3 years ago

import java.util.*; public class Solution { private boolean[] visited; // To keep track of visited nodes public boolean validPath(int n, int[][] edges, int start, int end) { if (edges.length == 0) return false; visited = new boolean[n]; visited[edges[0][0]] = true; for (int i = 0; i < edges.length; i++) { if (visited[edges[i][0]] == true) { visited[edges[i][1]] = true; } } return visited[start] == true && visited[end] == true; } }
Show 1 reply

Reading Progress

0%


Vote for new content