Imagine you're exploring a mysterious mansion with n rooms labeled from 0 to n-1. All rooms are locked except for room 0, which serves as your starting point. Your mission is to visit every single room in the mansion.
Here's the catch: you can only enter a locked room if you have its specific key! Fortunately, each room you visit might contain a set of keys that unlock other rooms. Each key has a number indicating which room it opens, and you can collect all the keys you find.
Input: An array rooms where rooms[i] contains all the keys you'll find in room i.
Output: Return true if you can visit all rooms, false otherwise.
Example: If rooms = [[1],[2],[3],[]], you start in room 0, find key 1, use it to enter room 1, find key 2, enter room 2, find key 3, enter room 3. All rooms visited โ return true!
Input & Output
Constraints
- n == rooms.length
- 2 โค n โค 1000
- 0 โค rooms[i].length โค 1000
- 1 โค sum(rooms[i].length) โค 3000
- 0 โค rooms[i][j] < n
- All the values of rooms[i] are unique