2Brute2Best
🔥0

Keys and Rooms

Medium·LeetCode #841 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

There are n rooms numbered 0 to n − 1. Room 0 starts unlocked, and every other room starts locked. Room i holds a list of keys, and a key with number k opens room k.

You start in room 0. Every time you enter a room, you can take all the keys in it, and you may enter any room whose key you already hold. Return true if you can enter every room, and false otherwise.

Input

A list rooms of length n. rooms[i] is the list of keys found inside room i.

Output

true if every room can be entered, otherwise false.

Example 1
Input: rooms = [[1],[2],[3],[]]
Output: true

Room 0 holds key 1, room 1 holds key 2, and room 2 holds key 3. Walking 0 → 1 → 2 → 3 opens everything.

Example 2
Input: rooms = [[1,3],[3,0,1],[2],[0]]
Output: false

Key 2 is only found inside room 2, and you need key 2 to enter room 2. Nobody can get in, so the answer is false.

Constraints
  • • n = rooms.length
  • • 2 ≤ n ≤ 1000
  • • 0 ≤ rooms[i].length ≤ 1000
  • • 1 ≤ sum of rooms[i].length ≤ 3000
  • • 0 ≤ rooms[i][j] < n
  • • All values in rooms[i] are unique.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“Start in room 0”
→ The search starts from one known node, so one traversal from that node is the whole job.
“keys found inside rooms”
→ Each room's list gives arrows out of that room. This is a directed graph, not a two-way one.
“enter every room”
→ Is every node reachable from the start? That is a count of visited nodes compared with n.
03

Approach Complexity Ladder

Brute Force — repeat passes until nothing opensO(N · (N + K))O(N)

Each pass collects the keys in open rooms, then opens every room whose key is held; stop when a pass opens nothing

Optimal — one DFS from room 0O(N + K)O(N)

Enter rooms with a stack, mark each room the first time it is entered, then compare the count with n

Brute Force Intuition

Keep two arrays: open[i] (I have been inside room i) and haveKey[k] (I hold key k). Each pass first picks up the keys in every open room, then opens every closed room whose key is held. Passes repeat until a pass opens nothing new. The answer is true when every room is open.

Recognition rule for this sub-pattern:

Adjacency list + visited set. Count how many times you must start a new traversal.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.