🚀 Day 20 of 100 Days LeetCode Challenge Problem: Minimum Absolute Difference in Sliding Submatrix Day 20 hits with a sliding window + sorting / data structure problem 🔥 💡 Key Insight: For every k x k submatrix, we need: 👉 Minimum absolute difference between any two distinct elements 🔍 Core Approach: 1️⃣ Slide Over All k x k Submatrices Traverse each possible top-left position (i, j) 2️⃣ Extract Elements Collect all elements inside the current submatrix 3️⃣ Sort Elements Sort the list → minimum difference will always be between adjacent elements 4️⃣ Find Minimum Difference Compute: min(arr[i] - arr[i-1]) for sorted array 👉 If all elements are same → answer = 0 💡 Optimization Thought: Instead of rebuilding every time: Use advanced structures like balanced BST / multiset Helps in efficient insert/remove while sliding 🔥 What I Learned Today: Sorting simplifies pair comparison problems Sliding window in 2D is a powerful technique Advanced data structures can optimize brute force 📈 Challenge Progress: Day 20/100 ✅ 20 Days of Consistency Completed! 🎯 LeetCode, Sliding Window, Matrix, Sorting, Data Structures, Optimization, Algorithms, DSA Practice, Problem Solving #100DaysOfCode #LeetCode #DSA #CodingChallenge #SlidingWindow #Matrix #Sorting #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
LeetCode Day 20: Minimum Absolute Difference in Sliding Submatrix
More Relevant Posts
-
🚀 LeetCode Day Problem Solving 🚀 Day-60 📌 Problem: Given a 2D grid of characters, check if there exists a cycle of same characters 🔁 ✔ Move only in 4 directions (up, down, left, right) ✔ Cycle length must be ≥ 4 ✔ Cannot go back to the immediate previous cell 🧠 Example: Input: grid = [ ["a","a","a","a"], ["a","b","b","a"], ["a","b","b","a"], ["a","a","a","a"] ] ✅ Output: true 📖 Explanation: ✔ There exists a loop of 'a' forming a valid cycle 💡 Key Insight: ✔ This is a Graph Cycle Detection problem in a Grid 👉 Treat each cell as a node 👉 Connect adjacent cells with same value ⚡ Approach (DFS / BFS): 1️⃣ Traverse every cell 2️⃣ If not visited → start DFS 3️⃣ While exploring neighbors: Move only to same character Track previous cell (parent) 4️⃣ If you reach a visited cell (not parent) → 🔥 Cycle detected 🚫 Important Condition: ✔ Ignore the immediate parent to avoid false cycle 📊 Complexity Analysis: ⏱ Time Complexity: O(m × n) 📦 Space Complexity: O(m × n) (visited + recursion stack) 🧠 What I Learned: ✔ Grid problems = Graph problems 🧠 ✔ Cycle detection using DFS + parent tracking ✔ Careful handling of revisits ✅ Day 60 Completed 🚀 Leveling up in Graphs + DFS on Grid 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
To view or add a comment, sign in
-
-
🚀 #LeetCode Day Problem Solving 🚀 Day-40 📌 Problem: Given a 2D grid of characters, check if there exists a cycle of same characters 🔁 ✔ Move only in 4 directions (up, down, left, right) ✔ Cycle length must be ≥ 4 ✔ Cannot go back to the immediate previous cell 🧠 Example: Input: grid = [ ["a","a","a","a"], ["a","b","b","a"], ["a","b","b","a"], ["a","a","a","a"] ] ✅ Output: true 📖 Explanation: ✔ There exists a loop of 'a' forming a valid cycle 💡 Key Insight: ✔ This is a Graph Cycle Detection problem in a Grid 👉 Treat each cell as a node 👉 Connect adjacent cells with same value ⚡ Approach (DFS / BFS): 1️⃣ Traverse every cell 2️⃣ If not visited → start DFS 3️⃣ While exploring neighbors: Move only to same character Track previous cell (parent) 4️⃣ If you reach a visited cell (not parent) → 🔥 Cycle detected 🚫 Important Condition: ✔ Ignore the immediate parent to avoid false cycle 📊 Complexity Analysis: ⏱ Time Complexity: O(m × n) 📦 Space Complexity: O(m × n) (visited + recursion stack) 🧠 What I Learned: ✔ Grid problems = Graph problems 🧠 ✔ Cycle detection using DFS + parent tracking ✔ Careful handling of revisits ✅ Day 40 Completed 🚀 Leveling up in Graphs + DFS on Grid 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency
To view or add a comment, sign in
-
-
Most problems don’t need complex data structures. They just need the right one. 📚 Today’s DSA Learning: Stack & Stack Pattern 💡 What is a Stack? A stack is a data structure that follows LIFO (Last In, First Out). 👉 The last element added is the first one to be removed. --- 💡 Basic Operations: - push() → add element - pop() → remove element - peek() → view top element --- 💡 When to use Stack? - Reversing elements - Undo/Redo operations - Expression evaluation - Backtracking problems --- 💡 Stack Pattern (Very Important) 🔹 How to identify it? - Problems involving “next greater/smaller element” - Need to track previous/next elements - Brackets validation - Monotonic behavior (increasing/decreasing) --- 💡 Common Stack Patterns: - Monotonic Stack (increasing/decreasing) - Balanced Parentheses - Next Greater Element - Histogram problems --- 💻 Practice (LeetCode): - #20 Valid Parentheses - #496 Next Greater Element I - #84 Largest Rectangle in Histogram - #155 Min Stack --- ⚡ Key Insight: Stack helps you process elements in a controlled order — especially when recent elements matter most. Still learning and improving every day 🚀 #DSA #leetcode #stack #algorithms #coding
To view or add a comment, sign in
-
-
🚀 Day 48/90 – Sliding Window Median | Building from Brute Force | DSA Pattern. Today’s focus was not on optimization, but on building the right foundation first. 🔴 Problem — Sliding Window Median (LeetCode 480) Given an array and a window of size k, → Slide the window across the array → Find the median of each window 💡 Approach — Brute Force (Foundation First) For every window: → Extract k elements → Sort the window → Compute median (handle odd & even cases carefully) ⏱️ Time Complexity: O((n − k) × k log k) 📦 Space Complexity: O(k) 🔑 Key Insight Before jumping to optimized solutions, it’s important to clearly understand: → How the window moves → How data changes at each step → How median is calculated correctly 🧠 What I Learned → Strong fundamentals simplify complex problems → Sliding window + sorting builds intuition for advanced approaches → Brute force is not useless — it’s the first step toward optimization 📌 Next Step Moving towards the optimized solution using heaps for efficient median tracking. Day 48 complete. On to Day 49 🚀 Staying consistent. #Day48of90 #LeetCode480 #SlidingWindow #DSAPatterns #CodingInterview #ProblemSolving #JavaDSA #LearnDSA #BruteForce #AlgorithmThinking
To view or add a comment, sign in
-
-
LeetCode Daily | Day 84 🔥 LeetCode POTD – 1559. Detect Cycles in 2D Grid (Medium) ✨ 📌 Problem Insight Given a 2D grid of characters: ✔ Move in 4 directions (up, down, left, right) ✔ Only move to cells with same value ✔ Detect a cycle of length ≥ 4 ✔ Cannot go back to immediate previous cell 🔍 Initial Thinking – Brute DFS ⚙️ 💡 Idea: ✔ Start DFS from every cell ✔ Track visited path ⚠️ Concern: ✔ Might revisit parent → false cycle ✔ Need a way to avoid backtracking confusion 💡 Key Observation 🔥 ✔ This is graph cycle detection in a grid ✔ While moving, track parent cell ✔ If we reach a visited cell ≠ parent → cycle found ✅ 🚀 Optimized Approach ✔ Use DFS with: → visited matrix → parent tracking (px, py) ✔ For each neighbor: → If not visited → continue DFS → If visited and not parent → cycle exists 🔧 Core Idea ✔ Grid = graph ✔ Same values = connected component ✔ Cycle detection = revisit node (not parent) ⏱ Complexity ✔ Time: O(m × n) ✔ Space: O(m × n) 🧠 Key Learning ✔ Always track parent in DFS cycle problems ✔ Grid problems often map to graph concepts ✔ Avoid false cycles by ignoring immediate back edge 🚀 Takeaway When dealing with grids, think like a graph — a small tweak like tracking parent turns a tricky problem into a standard cycle detection pattern ⚡ #LeetCode #DSA #Algorithms #GraphTheory #CPlusPlus #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 83/100 – Subsets II 🧠 Problem: Given an integer array that may contain duplicates, return all possible subsets (power set). ⚠️ The solution set must not contain duplicate subsets 💡 Core Idea This is Backtracking + Duplicate Handling 🔥 1️⃣ First sort the array 2️⃣ Use Pick / Not Pick approach 3️⃣ While skipping elements → avoid duplicates: 👉 Skip all same elements using a loop 4️⃣ Build subsets recursively 👉 Key trick: Skip duplicates carefully 📚 Key Learnings Handling duplicates in recursion Importance of sorting Avoiding redundant subsets ⏱️ Complexity Time Complexity: O(2ⁿ) Space Complexity: O(n) #100DaysOfCode #Day83 #LeetCode #DSA #Backtracking #Recursion #Subsets #Algorithms #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 9 — Check if the Array is Sorted Continuing my journey of improving problem-solving skills with consistency and discipline — one problem every day. Today’s problem focused on verifying order — a simple concept, but very important in many algorithms. 🧩 Problem Solved: • Check if the Array is Sorted 📚 Topic: Arrays (Traversal) 💡 Key Insight: To check if an array is sorted, we only need to ensure that every element is less than or equal to the next one. ⚡ Approach: • Traverse the array from start to end • Compare each element with the next • If any element is greater than the next → return False • If no violations found → return True 🎯 Takeaway: Simple checks like this are often building blocks for more complex algorithms like binary search and sorting techniques. ⏱ Complexity: • Time → O(n) • Space → O(1) 💻 Example: Input → [1, 2, 3, 4, 5] → Output → True Input → [1, 2, 1, 4, 5] → Output → False Strong fundamentals make advanced problems easier 🚀 #ProblemSolving #Arrays #CodingJourney #100DaysOfCode #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 80/100 – Word Search 🧠 Problem: Given a 2D grid of characters and a word, return true if the word exists in the grid. ✔️ You can move: up, down, left, right ❌ You cannot reuse the same cell 💡 Core Idea This problem is solved using Backtracking + DFS 🔥 1️⃣ Start from every cell in the grid 2️⃣ If the first character matches → start DFS 3️⃣ Explore all 4 directions 4️⃣ Mark the cell as visited (temporarily) 5️⃣ Backtrack by restoring the original value 👉 Match → Explore → Mark → Undo 📚 Key Learnings Depth First Search (DFS) on grid Backtracking with visited marking Handling constraints in matrix problems ⏱️ Complexity Time Complexity: O(m × n × 4^L) (L = length of word) Space Complexity: O(L) #100DaysOfCode #Day80 #LeetCode #DSA #Backtracking #DFS #Matrix #Algorithms #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🔥 Day 172 of My LeetCode Journey Problem 129: Sum Root to Leaf Numbers 💡 Problem Insight: Today’s problem was about forming numbers from root-to-leaf paths in a binary tree and returning their total sum. Each path represents a number, so the challenge is building that number correctly while traversing. 🧠 Concept Highlight: The solution uses DFS with path accumulation: At each node, update the current value → current = current * 10 + node.val Traverse left and right When you reach a leaf, add the formed number to the total No need to store paths explicitly, just carry the value forward. 💪 Key Takeaway: Instead of storing full paths, carry forward the computed state. This keeps the solution clean, memory-efficient, and easy to reason about. ✨ Daily Reflection: This problem reinforced how tree traversal, combined with simple math, can solve seemingly complex problems. Understanding flow matters more than storing data. #Day172 #LeetCode #BinaryTree #DFS #PathSum #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Day Problem Solving 🚀 Day-65 📌 Problem: Given an array nums[] 🎯 Define rotation function: 👉 Rotate array k times → arrₖ 👉 Compute: F(k) = 0*arrₖ[0] + 1*arrₖ[1] + ... + (n-1)*arrₖ[n-1] 🎯 Goal: Find the maximum value among all F(k) 🧠 Example: Input: nums = [4,3,2,6] ✅ Output: 26 💡 Key Insight (Most Important 🔥): ✔ Brute force rotation = ❌ O(n²) (too slow) 👉 Instead, use relation between consecutive rotations ⚡ Core Formula: F(k) = F(k-1) + sum(nums) - n \cdot nums[n-k] 🔥 Meaning: ✔ We can compute next rotation using previous one ✔ No need to recompute from scratch ⚡ Approach: 1️⃣ Compute: sum = total sum of array F(0) 2️⃣ Iterate k = 1 → n-1: Use formula to compute F(k) 3️⃣ Track maximum value ⚡ Why it works? ✔ Each rotation shifts elements ✔ Contribution changes in a predictable way 👉 That’s why recurrence works 🚀 📊 Complexity Analysis: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Turning brute force → optimized using math ✔ Rotation problems often have hidden patterns ✔ Importance of deriving recurrence relations ✅ Day 65 Completed 🚀 Leveling up in Array + Mathematical Optimization 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀
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