Skip to content

Commit d77a90a

Browse files
committed
841: add a solution in Python3
1 parent 7eeb25e commit d77a90a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

841.Keys_and_Rooms/main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def canVisitAllRooms(self, rooms):
3+
"""
4+
:type rooms: List[List[int]]
5+
:rtype: bool
6+
"""
7+
8+
locks = [1] + [0] * (len(rooms) - 1)
9+
keys = set()
10+
11+
for key in rooms[0]:
12+
keys.add(key)
13+
found = True
14+
15+
while found:
16+
found = False
17+
for i in range(len(rooms)):
18+
if locks[i] == 0:
19+
if i in keys:
20+
locks[i] = 1
21+
for key in rooms[i]:
22+
keys.add(key)
23+
found = True
24+
25+
if 0 in locks:
26+
return False
27+
else:
28+
return True

0 commit comments

Comments
 (0)