🚀 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
LeetCode 841 Keys and Rooms Solution with DFS and BFS
More Relevant Posts
-
Day 105 - LeetCode Journey Solved LeetCode 933: Number of Recent Calls ✅ Simple problem, but a great use of queue + sliding window concept. Idea: Store timestamps in a queue Remove all calls older than t - 3000 Remaining size = answer Key learnings: • Sliding window using queue • Maintaining only relevant data • Efficient real-time processing • Clean O(n) approach ✅ All test cases passed ⚡ O(1) amortized per operation Sometimes clean logic is all you need 💡 #LeetCode #DSA #Queue #SlidingWindow #Java #CodingJourney #ProblemSolving #InterviewPrep
To view or add a comment, sign in
-
-
Today I solved LeetCode 110 – Balanced Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is height-balanced. A binary tree is balanced if: The height difference between the left and right subtree of every node is not more than 1. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Height calculation 🧠 Approach: Use DFS to calculate the height of each subtree. For every node: Get height of left subtree Get height of right subtree Check if the absolute difference is greater than 1: If yes → tree is not balanced Optimize by returning -1 early when imbalance is found to avoid unnecessary calculations. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Combining height calculation with validation in one traversal. Optimizing recursive solutions with early stopping. Understanding balanced vs unbalanced trees. Strengthening DFS and recursion skills. Improving step by step with tree problems Consistency is building confidence every day #LeetCode #DSA #BinaryTree #BalancedTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 51/100 – LeetCode Challenge Problem: Third Maximum Number Today I solved the “Third Maximum Number” problem, which focuses on identifying the third distinct maximum value in an array. The key challenge here was handling duplicates while keeping track of the top three distinct values. Instead of sorting the array directly, I used a set to eliminate duplicates first. This simplified the problem by ensuring that only unique values were considered. From there, I handled two cases. If there were fewer than three distinct numbers, I returned the maximum. Otherwise, I iterated through the set and tracked the top three maximum values using variables, updating them as I encountered larger numbers. This approach avoids unnecessary sorting and keeps the logic efficient and controlled. The solution runs in O(n) time with O(n) space complexity due to the set. This problem reinforced the importance of handling duplicates carefully and thinking about edge cases before jumping into implementation. Fifty-one days in. The focus is now on writing cleaner logic with fewer assumptions. #100DaysOfLeetCode #Java #DSA #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 70/100 🚀 | #100DaysOfDSA Solved LeetCode 75 – Sort Colors today. The problem was to sort an array containing only 0s, 1s, and 2s in-place without using any built-in sort. Approach: Used the Dutch National Flag Algorithm (3-pointer approach). • Maintained three pointers: left (for 0s), mid (current), and right (for 2s) • Traversed the array using mid If nums[mid] == 0 → swap with left, move both left and mid If nums[mid] == 1 → just move mid If nums[mid] == 2 → swap with right, move right only • Continued until mid > right This ensures all 0s, 1s, and 2s are placed correctly in a single pass. Time Complexity: O(n) Space Complexity: O(1) Key takeaway: Using multiple pointers to partition an array in one pass is a powerful technique for in-place sorting problems. #100DaysOfDSA #LeetCode #DSA #Java #Arrays #TwoPointers #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 66/100 🚀 | #100DaysOfDSA Solved LeetCode 14 – Longest Common Prefix today. The task was to find the longest common prefix string among an array of strings. Initially, I couldn’t come up with this optimal approach and first solved it using a brute-force O(n²) method, comparing characters across all strings. Approach (Optimized): Used sorting to simplify the problem. • Sorted the array of strings. • Took the first and last strings after sorting. • Compared characters of both strings until they differ. • The common prefix between these two will be the answer for all strings. This works because after sorting, the most different strings will be at the extremes. Time Complexity: O(n log n + m) (where n = number of strings, m = length of prefix comparison) Space Complexity: O(1) (ignoring sorting space) Key takeaway: Sorting can sometimes reduce multi-string comparison problems to just comparing two extreme cases, making the solution much simpler and efficient. #100DaysOfDSA #LeetCode #DSA #Java #Strings #Sorting #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 113 - LeetCode Journey Solved LeetCode 222 – Count Complete Tree Nodes ✅ This problem involves counting the total number of nodes in a complete binary tree, where all levels are fully filled except possibly the last, and nodes are as far left as possible. Approach: I implemented a recursive solution to count nodes. For each node, I recursively calculated the number of nodes in the left and right subtrees and added 1 for the current node. Although this solution works correctly, it follows a straightforward traversal approach and does not fully utilize the properties of a complete binary tree. A more optimized approach can reduce time complexity by comparing left and right subtree heights to detect perfect subtrees and compute their node count directly. Complexity Analysis: • Time Complexity: O(n) • Space Complexity: O(h), where h is the height of the tree (recursion stack) Key Takeaways: • Basic tree traversal (DFS) can solve counting problems effectively • Understanding tree properties can help in optimizing solutions further • Complete binary trees allow more efficient approaches than general trees All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 43 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Sum of Unique Elements Problem Insight: Find elements in an array that appear exactly once and calculate their total sum. Approach: • Used a frequency array to count occurrences of each number • Traversed the array to build frequency • Added only those elements to sum whose frequency is exactly 1 Time Complexity: • O(n) Space Complexity: • O(1) (fixed size array used for constraints) Key Learnings: • Frequency array is faster than HashMap when range is fixed • Two-pass approach makes logic clear and simple • Always check constraints before choosing data structure Takeaway: Right data structure makes the solution simple and efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 27/100 – LeetCode DSA Challenge 📌 Problem Solved: Find All Numbers Disappeared in an Array Today’s problem focused on identifying missing numbers from an array where elements are in the range [1, n]. 🔍 Approach I Used (Easy Method): Created a boolean array to track visited numbers Marked each number as present Iterated from 1 to n to find missing elements 💡 Key Concepts Learned: How to use index-based mapping for arrays Importance of range constraints [1, n] Difference between brute force vs optimized approach Using extra space to simplify logic 🧠 What I Understood Today: If a problem says numbers are in range [1, n], we can directly map values to indices Tracking frequency or presence is a powerful technique Writing clean and simple logic first is more important than jumping to optimization 🔥 Next Step: I will try the optimized approach (O(1) space) using index marking (negative marking technique) 📈 Improving step by step in DSA — consistency is the key! #Day27 #LeetCode #DSA #100DaysOfCode #CodingJourney #PlacementsPreparation #Java
To view or add a comment, sign in
-
-
🚀 Day 47of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Mirror Distance of an Integer Problem Insight: Given a number, the task is to find the absolute difference between the original number and its reversed form. Approach: • Stored the original number in a temporary variable • Reversed the number using digit extraction (modulo and division) • Calculated the absolute difference between the original and reversed number Time Complexity: O(d), where d = number of digits Space Complexity: O(1) Key Learnings: • Digit manipulation using modulo and division is a powerful technique • Always store the original value before modifying the input • Reversing numbers is a fundamental pattern in many DSA problems Takeaway: Breaking the problem into simple steps makes even tricky-looking logic easy to solve. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 48 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Sort Array By Parity Problem Insight: Rearrange the array so that all even numbers come before odd numbers. The order of elements doesn’t matter. Approach: • Used the two-pointer technique (partition logic) • Maintained a pointer j to track the position for even numbers • Traversed the array using i • Whenever an even number is found, swapped it with index j and incremented j • This ensures all even elements move to the front in a single pass Time Complexity: O(n) Space Complexity: O(1) (in-place solution) Key Learnings: • Two-pointer technique is very useful for array partitioning problems • Swapping helps avoid extra space usage • This pattern is similar to problems like moving zeros or segregating positives/negatives Takeaway: Simple logic + optimal approach = clean and efficient solution. Consistency is making these patterns easier to recognize! #DSA #Java #LeetCode #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development