Keys and Rooms
There are n locked rooms numbered from 0, and only room 0 is open. Each room has some keys lying in it, each opening one room. Find out whether you can get into every room. Our example: the keys in rooms 0 to 4 are [1, 3], [3, 0], [4], [], [2] — the answer is false.
- input
rooms:nrooms numbered from0, all locked but room0.rooms[i]are the keys lying in roomi: a keykopens roomk.- returns
trueif you can get into every room, taking the keys from the rooms you have opened; otherwisefalse.- constraints
- n = rooms.length, 2 ≤ n ≤ 400
- 0 ≤ rooms[i][j] < n
- the keys in a room are all different
- 1 ≤ sum(rooms[i].length) ≤ 2000
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[][]} rooms
* @return {boolean}
*/
function canVisitAllRooms(rooms) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more breadth-first search (bfs) problems
- 637Average of Levels in Binary TreeEasysolve
- 102Binary Tree Level Order TraversalMediumsolve
- 199Binary Tree Right Side ViewMediumsolve
- 994Rotting OrangesMediumsolve
- 1091Shortest Path in Binary MatrixMedium
- 117Populating Next Right Pointers in Each Node IIMediumsolve
- 103Binary Tree Zigzag Level Order TraversalMediumsolve
- 909Snakes and LaddersMediumsolve
- 433Minimum Genetic MutationMediumsolve
- 127Word LadderHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.