Graph Theory37 sections · 1633 units
Open in Course

LeetCode 841 Keys and Rooms - Mapping

Translating to Graphs

Each room is a vertex, and each key in a room creates a directed edge to another room. Room 00 has keys [1, 3] means edges 010 \to 1 and 030 \to 3.

You start in room 00, so that is your source vertex. Run DFS to see which rooms you can reach. If you visit all nn rooms, return true.

This translation makes the problem trivial. It is a standard "can I reach all vertices from the source?" question.