Keys and Rooms

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Keys and Rooms

Approach

Treat each room as a node and each key as a directed edge. DFS/BFS through keys and count visited nodes.

Complexity Analysis

Time
O(N + K)
Space
O(N)

Pattern

Graph Reachability

Why It Works

All possible moves are key edges. If every room is eventually visited, then keys connect the graph from node 0 to all nodes.

Updated Feb 2026