Keys and Rooms

Problem Id: 841 Difficulty: Medium


Intuition

Solution


class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        n = len(rooms)
        self.visited = [False] * n

        self.dfs(rooms, 0)
        return all(self.visited)

    def dfs(self, graph, vertex):
        self.visited[vertex] = True
        for child in graph[vertex]:
            if not self.visited[child]:
                self.dfs(graph, child)