Problem
There are n rooms with the numbers 0 to n - 1. Room 0 is open. All other rooms are locked at the start.
rooms[i] is the list of keys in room i. A key with the number k opens room k. When you enter a room, you take all keys in it. You can visit the open rooms in any order, and you can visit a room again.
Return true if you can visit every room. Otherwise, return false.
Example 1
- Input:
- rooms = [[2],[],[1,3],[]]
- Output:
- true
- Explanation:
- Room 0 has the key to room 2. Room 2 has the keys to rooms 1 and 3.
Example 2
- Input:
- rooms = [[1],[0],[3],[2]]
- Output:
- false
- Explanation:
- Rooms 0 and 1 only have keys to each other. Nobody can open room 2 or room 3.
Example 3
- Input:
- rooms = [[],[0]]
- Output:
- false
Constraints
- n == rooms.length
- 2 <= n <= 1000
- 0 <= rooms[i].length <= 1000
- 1 <= sum(rooms[i].length) <= 3000
- 0 <= rooms[i][j] < n
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 841. Keys and Rooms
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source