Skip to content

Keys and Rooms

MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

There are n locked rooms numbered 0 to n - 1, except room 0, which is open. rooms[i] lists the keys you find inside room i; a key with number j opens room j. You may enter rooms in any order once you hold their key. Return true if you can visit every room, and false otherwise.

rooms = [[1], [2], [3], []]          →  true    (0 → 1 → 2 → 3)
rooms = [[1, 3], [3, 0, 1], [2], [0]] →  false   (nobody holds the key to room 2)

Constraints

  • 2 <= n <= 1000
  • 0 <= rooms[i].length <= 1000
  • The total number of keys across all rooms is at most 3000
  • Every key is a valid room number