-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path841.py
25 lines (25 loc) · 1004 Bytes
/
841.py
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
__________________________________________________________________________________________________
sample 60 ms submission
class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
q, seen = [0], {0}
for node in q:
for nei in rooms[node]:
if nei not in seen:
q.append(nei)
seen.add(nei)
return len(seen) == len(rooms)
__________________________________________________________________________________________________
sample 13296 kb submission
class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
st = [0]
entered = set([0])
while st:
c = st.pop(-1)
for k in rooms[c]:
if k not in entered:
st.append(k)
entered.add(k)
return len(entered) == len(rooms)
__________________________________________________________________________________________________