Advertisement

Keys and Rooms - LeetCode 841 Solution

Keys and Rooms - Complete Solution Guide

Keys and Rooms is LeetCode problem 841, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0 . Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key. When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms. Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i , return true if yo

Detailed Explanation

The problem asks whether it's possible to visit all rooms in a building, given a list of rooms and the keys that can be found in each room. You start in room 0, which is unlocked. Each room contains a list of keys, where each key represents a room number that can be unlocked. The goal is to determine if you can visit all the rooms using the keys you find along the way. The input is a list of lists called `rooms`, where `rooms[i]` is the list of keys you find in room `i`. The output is a boolean value: `true` if you can visit all rooms, and `false` otherwise. The constraints specify the size of the input and valid ranges for the room and key numbers.

Solution Approach

The provided solutions use Depth-First Search (DFS) with a stack to traverse the rooms. We start with room 0 and mark it as visited. Then, we iterate through the keys found in that room. For each key, if the corresponding room hasn't been visited yet, we mark it as visited and add it to the stack. We continue this process until the stack is empty, indicating that we've explored all reachable rooms. Finally, we check if the number of visited rooms equals the total number of rooms. If it does, we return `true`; otherwise, we return `false`.

Step-by-Step Algorithm

  1. Step 1: Initialize a `visited` set (or array) to keep track of the visited rooms, and add room 0 to the set (since we start there).
  2. Step 2: Initialize a stack and push room 0 onto the stack.
  3. Step 3: While the stack is not empty:
  4. Step 4: Pop a room from the stack.
  5. Step 5: Iterate through the keys found in the popped room.
  6. Step 6: For each key, check if the corresponding room has been visited.
  7. Step 7: If the room hasn't been visited, add it to the `visited` set and push it onto the stack.
  8. Step 8: After the stack is empty, check if the size of the `visited` set equals the total number of rooms. If it does, return `true`; otherwise, return `false`.

Key Insights

  • Insight 1: The problem can be modeled as a graph, where rooms are nodes and keys represent edges between rooms.
  • Insight 2: We can use Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the graph and find all reachable nodes (rooms).
  • Insight 3: We need to keep track of which rooms have been visited to avoid infinite loops and ensure we explore each room only once. Using a `visited` set or array is crucial.

Complexity Analysis

Time Complexity: O(N+E)

Space Complexity: O(N)

Topics

This problem involves: Depth-First Search, Breadth-First Search, Graph.

Companies

Asked at: Expedia, Graviton, Nvidia, Tinkoff, Walmart Labs.