We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 49e8489 commit d846efeCopy full SHA for d846efe
search/841_canVisitAllRooms.py
@@ -0,0 +1,37 @@
1
+import collections
2
+from typing import List
3
+
4
5
+class Solution:
6
+ def canVisitAllRooms_bfs(self, rooms: List[List[int]]) -> bool:
7
+ n = len(rooms)
8
+ vis = {0}
9
+ q = collections.deque()
10
+ q.append(0)
11
+ num = 0
12
13
+ while q:
14
+ x = q.popleft()
15
+ num += 1
16
17
+ for i in rooms[x]:
18
+ if i not in vis:
19
+ vis.add(i)
20
+ q.append(i)
21
22
+ return num == n
23
24
+ def canVisitAllRooms_dfs(self, rooms: List[List[int]]) -> bool:
25
+ def dfs(x):
26
+ vis.add(x)
27
+ nonlocal num
28
29
+ for it in rooms[x]:
30
+ if it not in vis:
31
+ dfs(it)
32
33
34
+ vis = set()
35
36
+ dfs(0)
37
+ return n == num
0 commit comments