Keys and Rooms LeetCode solution in Java

1
class Solution {
2
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
3
boolean[] visited = new boolean[rooms.size()];
4
visited[0] = true;
5
for (int a : rooms.get(0)) {
6
if (!visited[a]) {
7
bfs(a, visited, rooms.size() - 1, rooms);
8
}
9
}
10
// System.out.println("arr -->>"+Arrays.toString(visited));
11
for (boolean a : visited) {
12
if (!a) return false;
13
}
14
return true;
15
}
16

17
public void bfs(int key, boolean[] vivsted, int target, List<List<Integer>> rooms) {
18

19
vivsted[key] = true;
20
for (int a : rooms.get(key)) {
21
if (!vivsted[a]) {
22
bfs(a, vivsted, target, rooms);
23
}
24
}
25
}
26
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0