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
Sliding Window Min Gap in 2D Matrix
More Relevant Posts
-
Day 80: The Vertical Flip 🔄 Problem 3643: Flip Square Submatrix Vertically Today’s task was all about in-place matrix manipulation. The goal: flip a k×k submatrix vertically within a larger grid, starting from a given (x,y) coordinate. The Strategy: • Two-Pointer Logic: Instead of creating a copy, I used a row-swapping approach. By iterating only halfway through the submatrix rows (k/2), I could swap the top rows with the bottom ones. • Coordinate Mapping: Carefully mapped the indices so that for every row i, its "mirror" row was x+k−1−(i−x). • In-Place Efficiency: This approach keeps the space complexity at O(1) while the time complexity stays a lean O(k²), making it as fast as physically possible to touch every element. Directly manipulating the grid is always more satisfying than allocating extra memory. It’s all about those clean, symmetrical swaps. 🚀 #LeetCode #Java #MatrixManipulation #Algorithms #ProblemSolving #DailyCode
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
-
-
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 🥷
To view or add a comment, sign in
-
-
Solved the Maximal Rectangle problem using a histogram + stack approach. The idea is to treat each row as the base of a histogram. For every row, we build a height array where each value represents consecutive 1’s vertically. Then, for each updated histogram, we apply the Largest Rectangle in Histogram algorithm using a monotonic stack. The stack stores indices of increasing heights. When a smaller height appears, we pop from the stack and calculate area using the popped height as the limiting height and width based on boundaries. This way, instead of checking all rectangles, we efficiently compute the maximum area row by row. Time Complexity: O(n * m) Space Complexity: O(m) #Java #DSA #Stack #LeetCode #Coding
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
-
-
Day 53/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Minimum Window Substring One of the most important sliding window problems. String properties: • We need a substring containing all characters of another string • Characters can repeat • The answer must be the smallest valid window Key idea: Use the sliding window technique with two pointers. Why? • Expanding the window helps include required characters • Shrinking the window helps find the minimum length • Efficiently balances both conditions Approach: • Store frequency of characters from string t • Use two pointers (left & right) to form a window • Expand right pointer to include characters • Decrease required count when a valid char is found • Once all chars are matched → try shrinking from left • Track the minimum window length during the process Time Complexity: O(n + m) Space Complexity: O(1) (constant for character set) Big takeaway: Sliding window is all about expand → satisfy → shrink → optimize. Sliding window concepts getting stronger day by day. 🔥 Day 53 done. #100DaysOfCode #LeetCode #DSA #Algorithms #SlidingWindow #Strings #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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 557 of #750DaysOfCode 🚀 🔥 Solved: XOR After Range Multiplication Queries II (Hard) Today’s problem really pushed my understanding of optimization and patterns in arrays. At first glance, it looks like a simple simulation—but with constraints up to 10⁵, a brute-force approach quickly breaks down. 💡 Key Learnings: Handling range updates efficiently is crucial Observed how step-based traversal (k jumps) affects time complexity Importance of modular arithmetic in large computations XOR properties helped in deriving the final result efficiently ⚡ Challenge: Each query updates elements at intervals, making it tricky to optimize without directly iterating every time. 🧠 Takeaway: Hard problems are less about coding and more about recognizing patterns and optimizing smartly. Consistency is the real game 💯 #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #Java #100DaysOfCode #KeepGrinding
To view or add a comment, sign in
-
-
Day 44 of Daily DSA 🚀 Solved LeetCode 1572: Matrix Diagonal Sum ✅ Problem: Given a square matrix mat, return the sum of the matrix diagonals. Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal. Approach: Used a two-pointer technique to traverse both diagonals in a single pass. Steps: Initialize two pointers → start = 0, end = n-1 Traverse each row: Add mat[i][start] (primary diagonal) Add mat[i][end] (secondary diagonal) Move pointers: start++, end-- If matrix size is odd → add center element once ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 46.53 MB Optimizing nested loops into a single pass can make your solution both cleaner and faster 💡 #DSA #LeetCode #Java #Arrays #Matrix #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝: 𝐃𝐞𝐭𝐞𝐫𝐦𝐢𝐧𝐞 𝐖𝐡𝐞𝐭𝐡𝐞𝐫 𝐌𝐚𝐭𝐫𝐢𝐱 𝐂𝐚𝐧 𝐁𝐞 𝐎𝐛𝐭𝐚𝐢𝐧𝐞𝐝 𝐁𝐲 𝐑𝐨𝐭𝐚𝐭𝐢𝐨𝐧 (𝟏𝟖𝟖𝟔) Today I solved an interesting matrix problem that involves matrix rotation and comparison. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦: Given two n × n binary matrices mat and target, determine whether mat can become target by rotating it in 90° increments (0°, 90°, 180°, 270°). 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Check if the current matrix equals the target. If not, rotate the matrix 90° clockwise. Repeat the process up to 4 rotations. If any rotation matches the target → return true. 𝐇𝐨𝐰 𝐫𝐨𝐭𝐚𝐭𝐢𝐨𝐧 𝐰𝐨𝐫𝐤𝐬: Step 1: Transpose the matrix (swap mat[i][j] with mat[j][i]) Step 2: Reverse every row This efficiently performs a 90° clockwise rotation in-place. Time Complexity: O(4 × n²) → effectively O(n²) Space Complexity: O(1) (in-place rotation) 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Matrix rotation problems can often be solved efficiently using the transpose + reverse technique, which is a powerful pattern for many 2D array problems. #LeetCode #DataStructures #Algorithms #Java #CodingPractice #ProblemSolving #SoftwareEngineering
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