[LeetCode] 841. Keys and Rooms

김민우·2022년 12월 20일
0

알고리즘

목록 보기
90/189

- Problem

841. Keys and Rooms

- 내 풀이

class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        N = len(rooms)
        graph = [[] for _ in range(N)]

        for i, v in enumerate(rooms):
            for j in v:
                graph[i].append(j)
        
        visited = [False]
        visited[0] = True
        q = collections.deque([0])

        while q:
            curr = q.popleft()
            for i in graph[curr]:
                if not visited[i]:
                    visited[i] = True
                    q.append(i)
        
        return all(visited)

- 결과

profile
Pay it forward.

0개의 댓글