#841
Keys and Rooms
pupil · 485 · lc medium +27 · verified · 75.5% accepted · 6,658 likes · top 86%
Description
There are n rooms numbered 0 through n - 1. All rooms are locked except room 0. To enter a locked room you must first obtain its key.
Each room may contain a set of keys. When you enter a room, you may collect all the keys inside and use them to unlock other rooms.
Given an array rooms where rooms[i] is the set of keys found in room i, return true if you can visit every room, or false otherwise.
Example 1:
Input: rooms = [[1],[2],[3],[]]
Output: true
Explanation:
We visit room 0 and pick up key 1.
We then visit room 1 and pick up key 2.
We then visit room 2 and pick up key 3.
We then visit room 3.
Since we were able to visit every room, we return true.
Example 2:
Input: rooms = [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can not enter room number 2 since the only key that unlocks it is in that room.
Code
1
2
3