-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeysAndRooms.java
More file actions
37 lines (29 loc) · 876 Bytes
/
Copy pathKeysAndRooms.java
File metadata and controls
37 lines (29 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class KeysAndRooms {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
Queue<Integer> nodes = new LinkedList<>();
boolean [] visited = new boolean[rooms.size()];
nodes.add(0);
while (!nodes.isEmpty()){
int node = nodes.poll();
if (!visited[node]) {
visited[node] = true;
for (int value : rooms.get(node)) {
if (!visited[value]){
nodes.add(value);
}
}
}
}
boolean state = true;
for (boolean v: visited ) {
state = state && v;
if (!state)
break;
}
return state;
}
}