Day: 73/365 📌 LeetCode POTD: Minimum Absolute Difference in Sliding Submatrix Medium Key takeaways/Learnings from this problem: 1. This problem shows how sliding window ideas extend to 2D, not just arrays, but it gets trickier to manage. 2. Maintaining a sorted structure (like multiset) helps in quickly finding min absolute differences in each window. 3. Big learning: brute force over every submatrix is too slow, so optimize by reusing previous window computations. 4. Overall, it’s a solid mix of 2D traversal + smart data structures to keep things efficient. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
Minimum Absolute Difference in Sliding Submatrix with Java
More Relevant Posts
-
Day 45 of Daily DSA 🚀 Solved LeetCode 867: Transpose Matrix ✅ Problem: Given a 2D matrix, return its transpose (rows become columns and columns become rows). Approach: Created a new matrix and swapped indices while traversing. Steps: Get number of rows and columns Create a new matrix of size cols x rows Traverse original matrix Assign: trans[j][i] = matrix[i][j] Return the new matrix ⏱ Complexity: • Time: O(n × m) • Space: O(n × m) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 46.46 MB Simple transformations like transpose build strong fundamentals for matrix-based problems 💡 #DSA #LeetCode #Java #Arrays #Matrix #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🔄 Solved “Spiral Matrix”, a classic matrix traversal problem using boundary-based approach. ✦ What This Problem Taught Me: • How to traverse a 2D matrix layer by layer using boundaries • Effective use of four pointers (top, bottom, left, right) • Managing direction-based traversal (→ ↓ ← ↑) • Importance of shrinking boundaries after each iteration • Avoiding duplicate traversals using proper conditions • Strengthened understanding of 2D arrays and indexing 🚀 Problems like this show how controlling boundaries and direction can simplify complex matrix traversals into a clean O(m × n) solution. On to solving more matrix and pattern-based problems 💪🔥 #LeetCode #DSA #Matrix #TwoPointers #CodingJourney #ProblemSolving #Java #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 50 of DSA – Minimum Absolute Difference in Sliding Submatrix Solved a matrix problem where for every k x k submatrix, we need to find the minimum absolute difference between any two distinct values. 💡 Key Insight For each submatrix: collect all values keep only distinct ones sort them the minimum absolute difference will always be between adjacent sorted values So instead of checking every pair, we can use a TreeSet to maintain sorted unique values directly. ⚡ Approach Iterate through every possible k x k window Store its elements in a TreeSet If only one distinct value exists, answer is 0 Otherwise, scan adjacent values in sorted order and compute minimum difference ⏱ Time Complexity: O((m-k+1)(n-k+1) * k² log(k²)) 💾 Space Complexity: O(k²) 📌 Lesson: Sometimes the best solution is not the most “fancy” sliding window optimization, but the one that matches the constraints cleanly and correctly. #DSA #LeetCode #Java #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
✅ Solved LeetCode 217 — Contains Duplicate! Given an integer array nums, return true if any value appears at least twice. 🧠 My Approach: Sort + Linear Scan → Sort the array → adjacent duplicates are guaranteed to be neighbors → One pass to check if nums[i] == nums[i-1] → Time: O(n log n) | Space: O(1) 💡 Key Insight: Sorting brings duplicates side by side — no need for extra space like a HashSet. Trade time for space! ```java Arrays.sort(nums); for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) return true; } return false; ``` 🔄 Alternative approaches: • HashSet → O(n) time, O(n) space • Brute force → O(n²) time (avoid!) Every problem teaches you a new trade-off. Keep grinding! 💪 #LeetCode #DSA #CodingInterview #Java #ProblemSolving #SoftwareEngineering #100DaysOfCode #Programming
To view or add a comment, sign in
-
-
Day 51/100 | #100DaysOfDSA 🚀⚡ Today’s problem: Product of Array Except Self Given an array, return a new array where each element is the product of all elements except itself — without using division. Brute force? Too slow. Division? Not allowed. Optimized Approach: • Build prefix product array (left to right) • Then multiply with suffix product (right to left) • No extra space needed (excluding output) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Breaking a problem into prefix + suffix patterns can unlock optimal solutions. Clean logic. Efficient approach. 💯 Day 51 done. 🔥 #100DaysOfCode #LeetCode #DSA #Algorithms #Arrays #PrefixSum #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🧠 Day 183 — Reverse Submatrix 🔄📊 Today solved a clean and intuitive matrix manipulation problem: Reverse a Submatrix. 📌 Problem Goal Given a matrix and parameters (x, y, k): ✔️ Select a k × k submatrix starting from (x, y) ✔️ Reverse it vertically (top ↔ bottom rows) 🔹 Core Idea Instead of touching every element randomly, we: 👉 Focus only on the selected submatrix boundaries 👉 Swap rows from top to bottom This works like reversing an array, but applied to rows inside a matrix window. 🔹 Approach 1️⃣ Set two pointers: • One at the top row • One at the bottom row 2️⃣ Swap entire rows (column by column) within the submatrix 3️⃣ Move inward until all rows are reversed 🔹 Why It Works We are effectively reversing the order of rows in the submatrix while keeping column positions intact. This ensures: ✔️ Correct transformation ✔️ In-place modification ✔️ Efficient traversal 🧠 Key Learning ✔️ Matrix problems often reduce to controlled pointer movement ✔️ Think in terms of sub-boundaries instead of full matrix ✔️ Row/column swaps are powerful tools for transformations 🚀 Momentum Status: Strong consistency with arrays, matrices, and trees. On to Day 184. #DSA #Matrix #ProblemSolving #Java #CodingJourney #LeetCode #ConsistencyWins
To view or add a comment, sign in
-
-
Day 89: Lane-Based Swapping 🏎️ Problem 2840: Check if Strings Can be Made Equal With Operations II Yesterday’s problem was a fixed-length vibe check; today, the constraints opened up. The mission: can we sync two strings of any length by swapping characters at any even-to-even or odd-to-odd indices? The Logic: • Two Separate Lanes: Characters at even indices can never move to odd positions. They live in two parallel universes. • Frequency over Simulation: Instead of actually swapping, I just checked if the "pool" of characters in each lane matched between both strings. • The Check: If the count of 'a'-'z' at all even positions in s1 matches s2, and the same holds for odd positions, any arrangement is reachable. It’s a classic case of seeing past the "swapping" distraction and realizing it’s just a frequency distribution problem. One more day down, logic still sharp. 🚀 #LeetCode #Java #Algorithms #DataStructures #DailyCode
To view or add a comment, sign in
-
-
Today I solved LeetCode 110 – Balanced Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is height-balanced. A binary tree is balanced if: The height difference between the left and right subtree of every node is not more than 1. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Height calculation 🧠 Approach: Use DFS to calculate the height of each subtree. For every node: Get height of left subtree Get height of right subtree Check if the absolute difference is greater than 1: If yes → tree is not balanced Optimize by returning -1 early when imbalance is found to avoid unnecessary calculations. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Combining height calculation with validation in one traversal. Optimizing recursive solutions with early stopping. Understanding balanced vs unbalanced trees. Strengthening DFS and recursion skills. Improving step by step with tree problems Consistency is building confidence every day #LeetCode #DSA #BinaryTree #BalancedTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
🚀Day 39 #LeetCode 199 – Binary Tree Right Side View Ever wondered what a binary tree looks like from the right side? 👀 Let’s break it down! 🧠 Problem Insight When you observe a binary tree from the right, at every level you only see one node — the rightmost node. 👉 So the goal is simple: Capture the last node at each level ⚡ Approach (Level Order Traversal - BFS) ✔ Traverse the tree level by level ✔ At each level, pick the last node ✔ Add it to the result 📊 Complexity ⏱ Time: O(n) 📦 Space: O(n) 🔥 Key Takeaway 👉 Rightmost node at each level = Visible node 💡 Example Input: [1,2,3,null,5,null,4] Output: [1,3,4] 💬 Have you tried solving this using DFS (Right-first traversal)? Drop your approach below! #LeetCode #DataStructures #BinaryTree #CodingInterview #Java #Programming
To view or add a comment, sign in
-
-
Day 79: Sliding Submatrix Grinds 🔍 Problem 3567: Minimum Absolute Difference in Sliding Submatrix Today’s challenge was about finding the smallest gap between unique values within a shifting k×k window. It’s essentially a "Sliding Window" problem on a 2D scale. The Strategy: • Window Traversal: Iterated through every possible top-left coordinate where a k×k submatrix could fit. • Flatten & Sort: For each window, I gathered all elements into a list and sorted them. Sorting is the shortcut here, the minimum difference must exist between adjacent elements in a sorted list. • The "Unique" Check: Carefully calculated the absolute difference between neighboring elements, ignoring duplicates to find the true minimum gap. While the nested loops + sorting approach got the job done today, it’s a bit of a brute-force flex. It’s a solid reminder that sometimes "Make it work" is the first step before "Make it fast." 🚀 #LeetCode #Java #Algorithms #DataStructures #DailyCode
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