gary@interview:~/interview/coding/841-keys-and-rooms.md$
$ cat ./coding/841-keys-and-rooms.md
[Coding]

841. Keys and Rooms

────────────────────────────────────────────────────────────

841. Keys and Rooms

透過拿到的鑰匙來造訪後面的房間,是圖形搜索的一種問題。

class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        visited = set()
        visited.add(0)
        queue = deque([0])

        while queue:
            room = queue.popleft()
            for key in rooms[room]:
                if key not in visited:
                    visited.add(key)
                    queue.append(key)
        
        return len(visited) == len(rooms)
--tags#Graph
$ ls ./coding/ | grep -v 841-keys-and-rooms
265. Paint House II256. Paint House143. Reorder List1762. Buildings With an Ocean View
← cd ../codingcd ~