Suppose we have a list of lists called rooms. Each index i in rooms represents a room and rooms[i] represents different keys to open other rooms. The room 0 is open and we are at that room and every other room is locked. We can move freely between opened rooms; we have to check whether we can open every room or not.
So, if the input is like rooms = [[2, 0], ,,], then the output will be True, as we start from room 0 and can go to room 2 with its key 2. From room 2 we can go to room 1. Then, take the key for room 3 and open it. So all are opened.
To solve this, we will follow these steps:
Let us see the following implementation to get better understanding:
class Solution: def solve(self, rooms): n = len(rooms) ready =  seen = set() while ready: u = ready.pop() seen.add(u) for v in rooms[u]: if v not in seen: ready.append(v) return len(seen) == n ob = Solution() rooms = [ [2, 0], , ,  ] print(ob.solve(rooms))
rooms = [[2, 0],,,]