🚀 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
Valid Path in Grid with Directional Constraints
More Relevant Posts
-
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 5 of #100Days 💻 Solved LeetCode 1391 – Check if There is a Valid Path in a Grid 🔍 Intuition: The problem looks like a grid traversal, but the key twist is direction compatibility. Each cell (street type) allows movement only in certain directions, and a move is valid only if both the current cell and the next cell agree on the connection (i.e., bidirectional connectivity). So, the idea is to treat the grid as a graph and perform BFS/DFS, while ensuring: The current cell allows movement in a direction The next cell allows movement back (reverse direction) This guarantees we only follow valid “pipes” and avoid invalid paths. ⏱ Time Complexity: O(m × n) — each cell is visited at most once #DSA #LeetCode #Graphs #BFS #ProblemSolving #CodingJourney
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 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 88 of #100DaysOfCode Solved 2058. Find the Minimum and Maximum Number of Nodes Between Critical Points 🔗 🧠 Key Insight: A node is a critical point if: 🔺 It is a local maxima → greater than both neighbors 🔻 It is a local minima → smaller than both neighbors ⚙️ Approach (Single Pass + Index Tracking): 1️⃣ Traverse the linked list while keeping track of: • prev, curr, next • current index i 2️⃣ Identify critical points: ✔️ curr > prev && curr > next ✔️ curr < prev && curr < next 3️⃣ Store their indices 4️⃣ If total critical points < 2 → return [-1, -1] 5️⃣ Otherwise compute: ✅ Minimum distance → between consecutive critical points ✅ Maximum distance → between first & last critical point ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(k) (critical points count) #100DaysOfCode #LeetCode #DSA #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 90/150 🚀 LeetCode 637: Average of Levels in Binary Tree 🧠 Problem Return the average value of nodes on each level in a binary tree. 💡 Approach • Use BFS (level order traversal) • For each level, calculate sum of nodes • Divide sum by number of nodes in that level • Store result in a vector ⏱ Time Complexity O(n) 📦 Space Complexity O(n) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day90 #LeetCode #BinaryTree #BFS #LevelOrder #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 36/60 – Reflected Binary Logic: Mastering Gray Code Generation! The final stretch of the #geekstreak60 is officially in sight! Today's challenge was all about Gray Codes—a sequence where each step only changes a single bit. The Technical Deep Dive: The Pattern: I used the Reflective Method. To generate an $n$-bit code, you take the $(n-1)$ code, mirror it, and append "0"s and "1"s as prefixes. Why it matters: Gray codes are essential in digital communications and error correction (like in rotary encoders) because they prevent "spurious" intermediate states during bit transitions. The Efficiency: This recursive approach builds the $2^n$ sequence in $O(2^n)$ time, which is optimal given the output size.
To view or add a comment, sign in
-
-
Leetcode POTD : Rotate Function Approch : These are the kind of problems where you need to calculate the answer of one index in O(n) and then use this precomputed answer to construct the rest answers in O(1) like here i calculated the answer for index 0 (considering 1 based indexing) and then used this value to calculate the answers for each value. Time Complexity : O(2n) Space Complexity : O(1)
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-61 📌 Problem: You are given an m × n grid where each cell represents a street type (1–6) 🛣️ 🎯 Determine if there exists a valid path from 👉 (0, 0) → (m-1, n-1) ✔ You can only move if streets are properly connected ❌ You cannot modify the grid 🧠 Example: Input: grid = [[2,4,3],[6,5,2]] ✅ Output: true 📖 Explanation: ✔ A valid connected path exists from start → end 💡 Key Insight: ✔ This is a Grid + Graph Traversal problem 👉 Each street type allows movement in specific directions ⚡ Street Connections: TypeDirections1Left ↔ Right2Up ↕ Down3Left ↔ Down4Right ↔ Down5Left ↔ Up6Right ↔ Up⚠️ Important Rule: 👉 Move from cell A → B only if: ✔ A allows movement toward B ✔ AND B allows movement back to A ⚡ Approach (BFS / DFS): 1️⃣ Start from (0,0) 2️⃣ Traverse using BFS / DFS 3️⃣ For each move: Check if direction is valid from current cell Check if next cell connects back 4️⃣ Mark visited to avoid loops 5️⃣ If reach (m-1, n-1) → ✅ true 📊 Complexity Analysis: ⏱ Time Complexity: O(m × n) 📦 Space Complexity: O(m × n) 🧠 What I Learned: ✔ Grid traversal with direction constraints ✔ Validating bidirectional connectivity ✔ BFS/DFS for path existence ✅ Day 61 Completed 🚀 Leveling up in Graph + Grid Traversal Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
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
-
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