-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-841.py
More file actions
31 lines (25 loc) · 695 Bytes
/
lc-841.py
File metadata and controls
31 lines (25 loc) · 695 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
'''
841. Keys and Rooms
Source: https://leetcode.com/problems/keys-and-rooms/
Algo: BFS
Type: Graph
Notes: This is a simple BFS problem.
Author: Rishabh IO
'''
class Solution:
def canVisitAllRooms(self, rooms) -> bool:
n = len( rooms )
locked = [ True ] * n
locked[ 0 ] = False
visited = set()
queue = [ 0 ]
while( queue ):
room = queue.pop(0)
visited.add( room )
locked[room] = False
for key in rooms[room]:
if key not in visited:
queue.append( key )
if any(locked):
return False
return True