LeetCode 841 Keys and Rooms Solution with DFS and BFS

🚀 LeetCode Problem Solved: Keys and Rooms (Day 43 of DSA Journey) Today, I tackled LeetCode 841 – Keys and Rooms, a great problem to strengthen understanding of graph traversal (DFS/BFS). 🔐 Problem Summary: We are given n rooms where only room 0 is initially unlocked. Each room may contain keys to other rooms. The goal is to determine whether we can visit all rooms using the available keys. 💡 Approach: Treat rooms as nodes and keys as edges Start traversal from room 0 Use DFS (Stack) or BFS (Queue) Track visited rooms to avoid repetition Finally, check if all rooms were visited 🧠 Key Insight: This problem is a classic example of checking whether a graph is fully reachable from a source node. ⏱️ Complexity: Time: O(n + e) Space: O(n) ✅ Outcome: Successfully implemented using DFS & BFS approaches. #LeetCode #DSA #Java #GraphTraversal #CodingJourney #100DaysOfCode

  • text

To view or add a comment, sign in

Explore content categories