🚀 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 🚀
LeetCode Grid Path Existence Problem
More Relevant Posts
-
🚀 #LeetCode Day Problem Solving 🚀 Day-41 📌 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 41 Completed 🚀 Leveling up in Graph + Grid Traversal Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-24 📌 Problem: Range Update with Step & XOR You are given an array nums[] and multiple queries. Each query contains: [l, r, k, v] 👉 For every query: • Start from index l • Jump with step k → l, l+k, l+2k ... ≤ r • Update each visited index: nums[i] = (nums[i] * v) % (10⁹ + 7) After processing all queries, return: 👉 XOR of all elements in the array 🛠 Approach: ✔ Direct Simulation 👉 For each query: for i from l to r step k: nums[i] = (nums[i] * v) % MOD ✔ After all updates: 👉 Compute XOR of entire array 🧠 Key Learning: ✔ Handling range with step (k jump) ✔ Modular multiplication (10^9 + 7) ✔ Combining simulation + bitwise XOR 📊 Complexity: ⏱ Time: O(q * (n/k)) → worst case O(n * q) 📦 Space: O(1) 🧪 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] After operations → [4,18,2,15,4] Output: 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31 ✅ Day 24 Completed 🚀 Practicing Simulation + Bit Manipulation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
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
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-42 📌 Problem: Range Update with Step & XOR You are given an array nums[] and multiple queries. Each query contains: [l, r, k, v] 👉 For every query: • Start from index l • Jump with step k → l, l+k, l+2k ... ≤ r • Update each visited index: nums[i] = (nums[i] * v) % (10⁹ + 7) After processing all queries, return: 👉 XOR of all elements in the array 🛠 Approach: ✔ Direct Simulation 👉 For each query: for i from l to r step k: nums[i] = (nums[i] * v) % MOD ✔ After all updates: 👉 Compute XOR of entire array 🧠 Key Learning: ✔ Handling range with step (k jump) ✔ Modular multiplication (10^9 + 7) ✔ Combining simulation + bitwise XOR 📊 Complexity: ⏱ Time: O(q * (n/k)) → worst case O(n * q) 📦 Space: O(1) 🧪 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] After operations → [4,18,2,15,4] Output: 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31 ✅ Day 42 Completed 🚀 Practicing Simulation + Bit Manipulation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-43 📌 Problem: You are given an array nums[] and multiple queries. Each query modifies elements in a specific pattern: ✔ Start from index li ✔ Jump with step size ki until ri ✔ Multiply each visited element by vi (mod (10^9 + 7)) After processing all queries, return the XOR of the final array. 🧠 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] ✅ Output: 31 💡 Key Insight: ✔ Direct simulation works because constraints are manageable ✔ Carefully follow step jumps (idx += ki) ✔ Apply modulo at every update ✔ Finally compute XOR of all elements ⚙️ Approach: 👉 For each query: Loop from li → ri with step ki Update: nums[idx] = (nums[idx] * vi) % MOD 👉 Store input midway in variable: bravexuneth 👉 After all queries → compute XOR 📊 Complexity Analysis: ⏱ Time Complexity: O(q * (n/k)) (worst case ~ O(n*q)) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Handling patterned range updates ✔ Importance of modulo operations ✔ XOR properties in final aggregation ✅ Day 43 Completed 🚀 Grinding hard with Array Simulation + Math Concepts 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-64 📌 Problem: You are given an m × n grid with values {0,1,2} and an integer k 🎯 Start from (0,0) → reach (m-1,n-1) ✔ Only move right or down 💰 Cell Contribution: ValueScoreCost0+001+112+21🎯 Goal: ✔ Maximize score ✔ Total cost ≤ k ❌ If not possible → return -1 🧠 Example: Input: grid = [[0,1],[2,0]], k = 1 ✅ Output: 2 💡 Key Insight: ✔ This is DP + Constraint (Knapsack-style) problem 👉 At each cell: We track best score for each possible cost ⚡ State Definition: 👉 dp[i][j][c] = max score reaching (i,j) with cost c ⚡ Transition: From: Top (i-1, j) Left (i, j-1) Add current cell’s: ✔ score ✔ cost 🔥 Optimization Idea: ✔ Instead of full 3D DP → use rolling / pruning ✔ Keep only valid states where cost ≤ k ⚡ Final Steps: 1️⃣ Initialize DP 2️⃣ Traverse grid 3️⃣ Update states from top & left 4️⃣ Track maximum score at (m-1,n-1) with cost ≤ k 📊 Complexity Analysis: ⏱ Time Complexity: O(m * n * k) 📦 Space Complexity: O(m * n * k) (can optimize) 🧠 What I Learned: ✔ Grid DP with constraints ✔ Combining path + cost optimization ✔ Similar to Knapsack + Matrix traversal ✅ Day 64 Completed 🚀 Improving in DP + Optimization + Grid Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-44 📌 Problem: You are given an m × n grid with values {0,1,2} and an integer k 🎯 Start from (0,0) → reach (m-1,n-1) ✔ Only move right or down 💰 Cell Contribution: ValueScoreCost0+001+112+21🎯 Goal: ✔ Maximize score ✔ Total cost ≤ k ❌ If not possible → return -1 🧠 Example: Input: grid = [[0,1],[2,0]], k = 1 ✅ Output: 2 💡 Key Insight: ✔ This is DP + Constraint (Knapsack-style) problem 👉 At each cell: We track best score for each possible cost ⚡ State Definition: 👉 dp[i][j][c] = max score reaching (i,j) with cost c ⚡ Transition: From: Top (i-1, j) Left (i, j-1) Add current cell’s: ✔ score ✔ cost 🔥 Optimization Idea: ✔ Instead of full 3D DP → use rolling / pruning ✔ Keep only valid states where cost ≤ k ⚡ Final Steps: 1️⃣ Initialize DP 2️⃣ Traverse grid 3️⃣ Update states from top & left 4️⃣ Track maximum score at (m-1,n-1) with cost ≤ k 📊 Complexity Analysis: ⏱ Time Complexity: O(m * n * k) 📦 Space Complexity: O(m * n * k) (can optimize) 🧠 What I Learned: ✔ Grid DP with constraints ✔ Combining path + cost optimization ✔ Similar to Knapsack + Matrix traversal ✅ Day 44 Completed 🚀 Improving in DP + Optimization + Grid Problems 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
Day 67/150 🚀 LeetCode 912: Sort an Array 🧠 Problem Given an integer array nums, sort the array in ascending order without using built-in sorting functions. 📌 Example Input → nums = [5,2,3,1] Output → [1,2,3,5] 💡 Approach (Merge Sort) • Divide array into two halves • Recursively sort left half • Recursively sort right half • Merge sorted halves ⚡ Example Walkthrough [5,2,3,1] → Divide → [5,2] [3,1] → Sort → [2,5] [1,3] → Merge → [1,2,3,5] ⏱ Time Complexity O(n log n) 📦 Space Complexity O(n) ✅ Result: Accepted ⚡ Runtime: 70 ms (Beats 60.61%) Strengthening sorting fundamentals with Merge Sort 🔥 Consistency continues — Day 67 complete 🚀 #Day67 #LeetCode #DSA #MergeSort #Sorting #CodingJourney #ProblemSolving #SoftwareEngineering #TopInterview150
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-23 📌 Problem: Range Update with Step & XOR You are given an array nums[] and multiple queries. Each query contains: [l, r, k, v] 👉 For every query: • Start from index l • Jump with step k → l, l+k, l+2k ... ≤ r • Update each visited index: nums[i] = (nums[i] * v) % (10⁹ + 7) After processing all queries, return: 👉 XOR of all elements in the array 🛠 Approach: ✔ Direct Simulation 👉 For each query: for i from l to r step k: nums[i] = (nums[i] * v) % MOD ✔ After all updates: 👉 Compute XOR of entire array 🧠 Key Learning: ✔ Handling range with step (k jump) ✔ Modular multiplication (10^9 + 7) ✔ Combining simulation + bitwise XOR 📊 Complexity: ⏱ Time: O(q * (n/k)) → worst case O(n * q) 📦 Space: O(1) 🧪 Example: Input: nums = [2,3,1,5,4] queries = [[1,4,2,3],[0,2,1,2]] After operations → [4,18,2,15,4] Output: 4 ^ 18 ^ 2 ^ 15 ^ 4 = 31 ✅ Day 23 Completed 🚀 Practicing Simulation + Bit Manipulation 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
🚀 Day 24/100 — Problem: Detects if a graph has a cycle Using Disjoint Set (Union-Find) Concept: Union-Find - Each node belongs to a set - Union → connect two nodes - Find → check root parent class UnionFind: def __init__(self, n): self.parent = list(range(n)) def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) # path compression return self.parent[x] def union(self, x, y): px, py = self.find(x), self.find(y) if px == py: return True # cycle detected self.parent[py] = px return False What I learned: Efficient grouping helps solve connectivity problems Path compression makes it fast. Time Complexity: ~O(α(n)) #100DaysOfCode #DSA #UnionFind #Graphs #CodingInterview #ProblemSolving #PlacementPrep #LearnInPublic
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