🚀 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
Valid Path in Grid with LeetCode 1391 Solution
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 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 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 13 of #LeetCode Challenge ✅ Question #1299 – Replace Elements with Greatest Element on Right Side 💡 Approach: Solved this problem by traversing the array from right to left while keeping track of the maximum element seen so far. 🔹 Steps: Initialize maxrt = -1 Traverse from last index to first Replace current element with maxrt Update maxrt = max(maxrt, current element) 🧠 What I Learned: Reverse traversal can make problems easier Maintaining a running maximum helps optimize to O(n) time complexity ⚡ Performance: Runtime: 3 ms Memory: 72.64 MB
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
-
-
🚀 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 116 Solved 1559. Detect Cycles in 2D Grid 🔁 Today’s problem was a great exercise in DFS + cycle detection in grids. 🔍 Key learnings: How to detect cycles while avoiding revisiting the immediate parent node Using DFS with direction vectors efficiently Tracking visited cells smartly to prevent false positives 💡 The tricky part was ensuring we don’t count the previous cell as a cycle — small detail, big impact. ⏱️ Performance: Runtime: 27 ms Memory: 64.79 MB Every day, a little sharper. On to Day 117 💪 #DSA #CodingJourney #100DaysOfCode #LeetCode #DFS #ProblemSolving
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
-
-
🚀 #GeekStreak60 | Day 47 Solved today’s POTD: Gray Code A classic bit manipulation problem where each consecutive pattern differs by exactly one bit. 🔍 Key Insight: Use formula: Gray Code = i ^ (i >> 1) This ensures only one bit changes between consecutive values. Time Complexity: O(2ⁿ × n) Space Complexity: O(2ⁿ) #geekstreak60 #npci #GeeksforGeeks #DSA #BitManipulation #CodingChallenge
To view or add a comment, sign in
-
-
👉Day 156 of 160 – LeetCode Challenge Problem: Path With Minimum Effort 👉 Problem:we have to return the minimum effort required to travel from the top-left cell to the bottom-right cell. 👉Key Concept: Use Dijkstra minimizing maximum edge difference, track effort with max(), relax neighbors, priority queue ensures optimal minimax path.
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