🚀 Day 245 of #500DaysOfCode Solved LeetCode 1559 – Detect Cycles in 2D Grid 🔄 💡 Approach: Traverse the grid using DFS Maintain a visited matrix While exploring neighbors: Move only in 4 directions (up, down, left, right) Only continue if the character matches Track parent cell (px, py) to avoid false cycle detection If we reach a visited cell that is not the parent → cycle found ✅ ⚡ Key Insight: Classic cycle detection in an undirected graph Parent tracking is crucial to avoid revisiting immediate previous node ⏱️ Complexity: Time: O(m × n) Space: O(m × n) (visited + recursion stack) ✨ Takeaway: Grid problems often map to graph traversal → think DFS/BFS + cycle detection patterns. Day 245 ✅ Still consistent 💪🔥 #LeetCode #DSA #DFS #Graphs #GridProblems #Consistency
Detecting Cycles in 2D Grid with DFS
More Relevant Posts
-
🚀 Day 70 of #100DaysOfCode Today, I solved LeetCode 1559 – Detect Cycles in 2D Grid, a problem that combines graph traversal with cycle detection in a matrix. 💡 Problem Overview: Given a 2D grid of characters, the task is to determine whether there exists a cycle formed by adjacent cells having the same value. 🧠 Approach: ✔️ Treated the grid as a graph where each cell is a node ✔️ Applied DFS/BFS to traverse connected components ✔️ Tracked the parent cell to detect cycles correctly ✔️ If a visited cell is encountered again (not the parent), a cycle exists ⚡ Key Takeaways: Cycle detection in grids requires careful parent tracking Matrix problems can be converted into graph problems DFS/BFS are versatile for both traversal and cycle detection 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(n × m) 70 days of consistency — building stronger problem-solving skills every day 🚀 #LeetCode #100DaysOfCode #DSA #Graphs #CycleDetection #DFS #BFS #ProblemSolving #CodingJourney #SoftwareDevelopment #Consistency
To view or add a comment, sign in
-
-
🚀 Day 32 – LeetCode 1559: Detect Cycles in 2D Grid (Medium) Solved a grid + DFS cycle detection problem today 🧠 We treat the grid as a graph and use DFS to check if there’s a cycle of same characters (length ≥ 4), making sure we don’t count immediate backtracking as a cycle. 🔑 Key idea: Grid = implicit graph 🔍 Technique: DFS + visited + parent tracking 💡 Lesson: Most grid problems are just graph problems in disguise. #LeetCode #DSA #DFS #Graphs #100DaysOfCode
To view or add a comment, sign in
-
-
Day 1️⃣ 3️⃣ /15 — Consistency Challenge 🚀 Today’s problem: LeetCode 1391 — Check if There is a Valid Path in a Grid A really interesting DFS + grid connectivity problem 🔥 🔹 Approach: Each cell represents a street type with specific allowed directions We simulate movement using DFS starting from (0, 0) Key Idea 💡 It’s not enough to just move to a neighbor — 👉 the next cell must also connect back to the current cell 🔹 Time Complexity: 👉 O(m × n) 🔹 Space Complexity: 👉 O(m × n) From simple grids → to directional graphs → to validation logic #LeetCode #DSA #DFS #Graphs #Consistency #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 8 of My LeetCode Journey Solved today’s problem: Detect Cycles in 2D Grid ✅ A classic graph problem involving DFS and cycle detection! 🔹 Problem Insight: Given a grid of characters, detect whether a cycle exists where adjacent cells have the same value. 🔹 Approach: • Treated the grid as a graph • Used DFS traversal to explore connected components • Tracked the previous cell to avoid false cycles • Detected a cycle if a visited cell is reached again (not the parent) 🔹 Complexity: • Time Complexity: O(m × n) • Space Complexity: O(m × n) 🧠 Key Learning: Cycle detection in grids is similar to graphs—tracking the parent node is crucial to avoid incorrect cycle detection. 📊 Today’s Stats: ✅ Runtime: 19 ms (Beats 78.50%) ✅ Memory: 70.92 MB (Beats 43.74%) Understanding graphs step by step 🚀 #LeetCode #DSA #Graph #DFS #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 1️⃣ 2️⃣ /15 — Consistency Challenge 🚀 Today’s problem: LeetCode 1559 — Detect Cycles in 2D Grid A really interesting one — combines DFS + cycle detection in graphs 🔥 🔹 Approach: Treat the grid like a graph where each cell connects to its 4 directions (up, down, left, right). We only move to adjacent cells having the same character. DFS Traversal: Maintaining a visited matrix For every unvisited cell → start DFS Cycle Detection Logic 💡 While exploring neighbors: If the neighbor is not visited → continue DFS If already visited and not the parent cell → cycle found ✅ 👉 Parent tracking (px, py) is the key to avoid false cycle detection DSA really connects everything together 💯 #LeetCode #DSA #DFS #Graphs #Consistency #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 246 of #500DaysOfCode Solved LeetCode 1391 – Check if There is a Valid Path in a Grid 🛣️ 💡 Approach: Each cell type defines allowed directions (streets) Use DFS/BFS traversal from (0,0) Move only if: Current cell allows that direction Next cell has a matching connection back Maintain a visited matrix to avoid cycles ⚡ Key Insight: It’s not just movement — it’s bidirectional connectivity validation Both cells must agree on the connection ⏱️ Complexity: Time: O(m × n) Space: O(m × n) ✨ Takeaway: Grid + constraints on movement → think graph traversal with validation rules. Another day, another step forward 💪🔥 #LeetCode #DSA #Graphs #DFS #GridProblems #Consistency
To view or add a comment, sign in
-
-
Day 72/150 🚀 LeetCode 104: Maximum Depth of Binary Tree 🧠 Problem Given the root of a binary tree, return its maximum depth. 📌 Example Input → root = [3,9,20,null,null,15,7] Output → 3 💡 Approach • Use recursion (DFS) • Calculate left subtree depth • Calculate right subtree depth • Return max(left, right) + 1 ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day72 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 181/365 – DSA Challenge 🌊 Solved Surrounded Regions on LeetCode today. 🔹 Problem: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. 👉 Replace all 'O' that are completely surrounded with 'X'. 🔹 Approach Used: DFS (Boundary Traversal) 💡 Key Idea: Any 'O' connected to the boundary can never be surrounded. Steps: 1️⃣ Traverse all boundary cells 2️⃣ Run DFS on boundary 'O' → mark them as safe 3️⃣ Traverse entire board: Unvisited 'O' → convert to 'X' Visited 'O' → keep as it is 🔹 Why this works? We avoid checking every region individually. Instead, we eliminate all safe regions first — much more efficient. 🔹 Time Complexity: O(m × n) 🔹 Space Complexity: O(m × n) (visited + recursion stack) 🔹 Concepts Used: DFS, Graph Traversal, Matrix Boundary Logic 🔥 Pattern Recognized: This is similar to problems like Number of Islands — but in reverse thinking (protect instead of count). 💻 Language: C++ Clean logic + strong intuition problem 🚀 #Day181 #365DaysOfCode #DSA #LeetCode #Graphs #DFS #CodingJourney #Cpp
To view or add a comment, sign in
-
-
Day 82/150 🚀 LeetCode 112: Path Sum 🧠 Problem Given a binary tree and a targetSum, return true if there exists a root-to-leaf path where the sum of node values equals targetSum. 💡 Approach • Use DFS (Recursion) • Keep adding node values while traversing • Check at leaf node if sum equals target • Return true if any path matches ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 3 ms 💾 Memory Beats: 94.93% #Day82 #LeetCode #BinaryTree #DFS #Recursion #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 83 of #100DaysOfCode Today, I solved LeetCode 1391 – Check if There is a Valid Path in a Grid, a problem that combines grid traversal with constraint-based movement. 💡 Problem Overview: Given a grid where each cell represents a type of street, the task is to determine whether there exists a valid path from the top-left cell to the bottom-right cell. 🧠 Approach: ✔️ Modeled the grid as a graph with directional constraints ✔️ Used BFS/DFS traversal to explore valid paths ✔️ Ensured movement is allowed only if both adjacent cells have compatible connections ✔️ Checked reachability of the destination cell ⚡ Key Takeaways: Grid problems often require validating movement constraints Not all neighbors are valid — direction compatibility matters BFS/DFS help in solving reachability problems efficiently 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(n × m) Improving logical thinking with every problem 🚀 #LeetCode #100DaysOfCode #DSA #Graphs #GridProblems #BFS #DFS #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
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