-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path841_Keys_and_Rooms.java
More file actions
36 lines (26 loc) · 855 Bytes
/
Copy path841_Keys_and_Rooms.java
File metadata and controls
36 lines (26 loc) · 855 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
class Solution {
void dfs(int curr,List<List<Integer>>rooms,ArrayList<Boolean>visited){
visited.set(curr,true);
for(int i : rooms.get(curr)){
if(!visited.get(i)){
dfs(i,rooms,visited);
}
}
// for(int i = 0;i<rooms.get(curr).size();i++){
// if(!visited.get(rooms.get(curr).get(i))){
// dfs(rooms.get(curr).get(i),rooms,visited);
// }
// }
}
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
ArrayList<Boolean> visited = new ArrayList<>();
for(int i = 0;i<rooms.size();i++){
visited.add(false);
}
dfs(0,rooms,visited);
for(Boolean b : visited){
if(!b)return false;
}
return true;
}
}