🚀 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
Detecting Cycles in 2D Grid with LeetCode 1559
More Relevant Posts
-
🚀 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 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
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 71 of #100DaysOfCode Today, I solved LeetCode 1572 – Matrix Diagonal Sum, a problem that focuses on matrix traversal and efficient computation. 💡 Problem Overview: Given a square matrix, the task is to calculate the sum of its primary and secondary diagonals, ensuring that the center element (if overlapping) is counted only once. 🧠 Approach: ✔️ Traversed the matrix to calculate: Primary diagonal → elements where row == column Secondary diagonal → elements where row + column == n - 1 ✔️ Handled the center element separately to avoid double counting ⚡ Key Takeaways: Understanding index relationships simplifies matrix problems Edge cases (like overlapping elements) are important Clean logic can avoid unnecessary loops 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) Small problems strengthen big concepts 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment
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 269 – Recursive Pattern Generation (Apr 2, 2026) 🔢🔁 🔹 Problems Solved Today: 1️⃣ LeetCode 38 – Count and Say 🔢 • Built the sequence iteratively from previous term. • Counted consecutive digits and formed the next string. • Repeated process until reaching the nth term. • Used two-pointer / run-length encoding style logic. • Reinforced pattern generation and string construction. 💡 Key Takeaways from Day 269: • Many sequence problems are based on previous state transformation. • Run-length encoding is a powerful pattern for grouping characters. • Iterative building is often simpler than recursion here. 🔥 Consistency Update: ✅ Day 269 streak maintained. 🎯 1006 problems solved so far. #LeetCode #DSA #ProblemSolving #Strings #Simulation #Consistency #Day269 🚀
To view or add a comment, sign in
-
-
🚀 Day 66 of #100DaysOfCode Today, I solved LeetCode 73 – Set Matrix Zeroes, a classic problem that tests in-place matrix manipulation and optimization techniques. 💡 Problem Overview: Given a matrix, if any cell contains 0, its entire row and column must be set to 0. The challenge is to perform this efficiently without using excessive extra space. 🧠 Approach: To solve this optimally, I focused on: ✔️ Using the first row and first column as markers ✔️ Tracking whether the first row/column initially contained zero ✔️ Updating the matrix in-place based on these markers This avoids using additional space and achieves optimal performance. ⚡ Key Takeaways: In-place algorithms help reduce space complexity Using matrix itself as storage is a powerful optimization trick Handling edge cases (first row/column) is critical 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(1) Improving problem-solving skills one day at a time 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
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 50/90: Cracking the Code on Binary Tree Views! We are officially past the halfway mark! 🚀 Today was all about changing perspectives—literally. Dealing with Binary Trees can get dizzying, but once you master the "views," the logic starts to feel like second nature. Here’s a breakdown of the logic I tackled today: 🔍 Problems Solved 1. Right Side View: Imagine standing to the right of the tree. Which nodes can you see? I used a modified DFS (Recursive) approach to prioritize the right child, ensuring I only grabbed the first node visible at each level. 2. Bottom-Left Most Element: This one is a classic. It’s not just the leftmost node; it’s the leftmost node on the very last level. A simple BFS (Level Order Traversal) made this a breeze. 3. Top View: This was the boss battle of the day! I used Vertical Level Order Traversal with a coordinate system (hd, level). By using a Map to store the first node encountered at each horizontal distance, I captured exactly what’s visible from the "sky." 💡 Key Takeaway: For tree "view" problems, Level Order Traversal is your best friend. If you can visualize the tree on a 2D coordinate plane, you can solve almost anything. Current Status: 50 days down, 40 to go. The momentum is real. #100DaysOfCode #DataStructures #Algorithms #BinaryTree #SoftwareEngineer #Vlog #CodingLife #LeetCode #TechJourney
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