841. Keys and Rooms
https://leetcode.com/problems/keys-and-rooms/
Python
DFS
class Solution:
def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
all_rooms = set(range(len(rooms)))
seen = set([0])
def dfs(room_id):
for key in rooms[room_id]:
if key not in seen:
seen.add(key)
dfs(key)
dfs(0)
return not bool(all_rooms - seen)