Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 750 Bytes

841.-keys-and-rooms.md

File metadata and controls

31 lines (24 loc) · 750 Bytes

841. Keys and Rooms

class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        
        q = collections.deque()
        q.append(0)
        graph = collections.defaultdict(list)
        for i,v in enumerate(rooms):
            graph[i].extend(v)
        ans = set([0])
        while q:
            ls = len(q)
            for _ in range(ls):

                room = q.popleft()
                if graph.get(room) is None:
                    continue

                keys = graph[room]
                for key in keys:
                    if key in ans:
                        continue
                    ans.add(key)
                    q.append(key)

        
        return len(ans) == len(rooms)