Leetcode 871 Solution

This article provides solution to leetcode question 871 (keys-and-rooms)

https://leetcode.com/problems/keys-and-rooms

Solution

class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        q = []
        q.append(0)
        seen = set()
        seen.add(0)

        while q:
            i = q.pop(0)
            for nei in rooms[i]:
                if nei not in seen:
                    q.append(nei)
                    seen.add(nei)

        return len(seen) == len(rooms)