Unlocking the Mystery of “Keys and Rooms” 🗝️🚪

Today, we dive deep into an intriguing problem from LeetCode, “841. Keys and Rooms”. It’s not just about keys and rooms … read on!

Tech Sauce
3 min readAug 26, 2023

📜 Problem Statement

You’re given an array of rooms, where each room is represented as a list of its keys. Each key opens a room. Initially, you are in room 0, and you have its keys. Can you visit all the rooms?

For example, given [[1],[2],[3],[]], the answer is true. Here's how:

  • Start in room 0, and pick up key 1.
  • Use key 1 to open room 1, and pick up key 2.
  • Use key 2 to open room 2, and pick up key 3.
  • Use key 3 to open room 3.

Voila! All rooms are visited!

But… what if you have something like [[1,3],[3,0,1],[2],[0]]? Is it still possible?

🤔 Intuition

Imagine a giant mansion with many rooms. Each room might have keys to other rooms. Your goal is to explore every room, starting from room 0. If you can find a path to visit every room at least once, then you've solved the puzzle!

In computer science terms, this mansion is just a graph. Rooms are nodes, and keys represent edges connecting the rooms. The problem boils down to: “Starting from node 0, can you traverse the entire graph?”

💡 Solution

One way to solve this is through Depth First Search (DFS).

  1. Begin at room 0.
  2. Explore each room you can access from your current room.
  3. If you’ve visited all rooms by the end, return true. Otherwise, false.

Sounds simple? Let’s dive into the code!

public class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
// Set to track visited rooms
Set<Integer> visited = new HashSet<>();

// Start DFS from room 0
dfs(0, rooms, visited);

// If visited rooms count is same as total rooms, return true
return visited.size() == rooms.size();
}

private void dfs(int room, List<List<Integer>> rooms, Set<Integer> visited) {
// Mark the current room as visited
visited.add(room);

// Go through all the keys in the current room
for (int key : rooms.get(room)) {
// If the room this key leads to hasn't been visited, visit it
if (!visited.contains(key)) {
dfs(key, rooms, visited);
}
}
}
}

📚 Explanation

  • We use a HashSet called visited to keep track of rooms we've been to.
  • We start our journey from room 0 using the dfs method.
  • Inside dfs, we mark the current room as visited.
  • Then, for every key in the room, if the corresponding room hasn’t been visited, we recursively explore it.
  • After we’ve finished our journey, if the number of rooms we’ve visited equals the total number of rooms, we return true; otherwise, false.

⏲️ Time Complexity

Every room is visited only once, and for each room, we look at all its keys. Hence, the time complexity is O(N + K), where N is the number of rooms and K is the total number of keys.

🌌 Space Complexity

In the worst case, we might end up storing all rooms in the visited set and the call stack (due to recursion) might grow up to N deep. Hence, the space complexity is O(N).

🎓 Conclusion

“841. Keys and Rooms” is a delightful introduction to graph traversal. Understanding how to navigate this mansion (graph) efficiently is a foundational skill in computer science. With the power of DFS in your toolkit, not only can you visit all rooms, but you can also conquer many complex coding challenges!

Further Learning at DSAGuide.com
For an in-depth exploration of data structures, algorithms, and coding interview solutions, I invite you to visit DSAGuide.com. Elevate your understanding with our detailed guides on essential data structures, algorithms and coding interview problem solving.

🌟 Stay Connected!

If you found this post helpful and wish to dive deep into more such intriguing problems and computer science concepts, please consider following my blog. Your support inspires me to share more such insights!

--

--

Tech Sauce
Tech Sauce

Written by Tech Sauce

Everything Software Engineering ...

No responses yet