Day 21/100 of DSA , (Arrays) 🚀Today I tackled the “Set Matrix Zeroes” problem At first, the brute force approach is tempting: traverse the matrix and use extra space to track zeros. Instead, I focused on the logic behind minimizing space: 🔹 Using the first row and first column as markers 🔹 Traversing carefully to avoid overwriting important values 🔹 Setting zeros in-place without extra arrays This problem is a reminder that optimization is not just about speed, but also about smart memory usage. 💡 Key takeaways: Think about what can be reused instead of creating new structures Always consider edge cases in 2D arrays Small logical adjustments make a big difference in performance Another step forward in mastering DSA fundamentals and improving problem-solving efficiency. 🚀 #DSA #Java #ProblemSolving #MatrixProblems #CodingJourney #InterviewPreparation #OptimizedCode
Mastering DSA: Optimizing Matrix Zeroes with Minimal Space
More Relevant Posts
-
🚀 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 51 of DSA – Determine Whether Matrix Can Be Obtained By Rotation Solved a matrix problem where we need to check whether one n x n binary matrix can be transformed into another by rotating it in 90-degree increments. 💡 Key Insight A square matrix can only have 4 possible rotational states: 0° 90° 180° 270° So instead of trying something complicated, we can simply: compare the current matrix with target rotate it by 90° clockwise repeat up to 4 times If any state matches, return true. ⚡ Approach Check whether mat is already equal to target If not, rotate the matrix clockwise Compare again after each rotation Since after 4 rotations the matrix comes back to the original form, no extra cases are needed ⏱️ Time Complexity: O(n²) 💾 Space Complexity: O(n²) #DSA #LeetCode #Java #Algorithms #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 346 – Daily DSA Challenge! 🔥 Problem: ⚡ Total Hamming Distance The Hamming distance between two integers is the number of bit positions where they differ. Given an integer array nums, return the sum of Hamming distances between all pairs. 💡 Key Insight — Count Bits Column-wise Instead of comparing every pair (O(n²)), analyze each bit position independently. For a given bit: Let ones = number of elements with that bit = 1 Let zeros = n - ones Each pair of (1,0) contributes 1 to Hamming distance. Total contribution for that bit: Sum this for all 32 bits. For each bit column we count ones × zeros and add them. ⚡ Algorithm ✅ Iterate through 32 bit positions ✅ Count how many numbers have that bit set ✅ Compute contribution ones × (n − ones) ✅ Add to result ⚙️ Complexity ✅ Time Complexity: O(32 × n) ≈ O(n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why does this approach avoid pairwise comparison? 2️⃣ How would this change for 64-bit numbers? 3️⃣ Can this be extended to compute XOR sum of all pairs? #DSA #Day346 #LeetCode #BitManipulation #HammingDistance #Math #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 12 – DSA / Logic Building Practice Today I worked on Building Block Stability (Stacking Problem) 🧱 📌 Problem: Given a sequence of blocks, check whether the structure remains stable when stacked in the given order. 💡 Approach: Used a single-pass check by comparing each block with the previous one. If any block is not smaller/lighter than the one below, the structure becomes unstable. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem helped me understand how simple validations can lead to optimal solutions without extra overhead. #Java #DSA #ProblemSolving #Algorithms #CodingInterview
To view or add a comment, sign in
-
-
🔥 Day 364 – Daily DSA Challenge! 🔥 Problem: 🎯 Find K Closest Elements Given a sorted array arr, an integer k, and a target x, return the k closest elements to x in ascending order. 💡 Key Insight — Binary Search on Window Instead of checking each element, we binary search the starting index of a window of size k. Search space: Possible windows → [0 ... n - k] 🧠 Decision Logic At index mid, compare: Distance of left boundary → x - arr[mid] Distance of right boundary → arr[mid + k] - x We choose direction based on: ⚡ Algorithm Steps ✅ Initialize: left = 0, right = n - k ✅ Binary search: If left side is farther → shift right Else → move left ✅ Final window starts at left ✅ Collect k elements ⚙️ Complexity ✅ Time Complexity: O(log(n - k) + k) (binary search + result building) ✅ Space Complexity: O(k) 💬 Challenge for you 1️⃣ Why do we search on window positions instead of elements? 2️⃣ Can you solve this using a heap approach? 3️⃣ What if array was unsorted? #DSA #Day364 #LeetCode #BinarySearch #SlidingWindow #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
Day 43/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Search a 2D Matrix II Interesting matrix pattern here. Matrix properties: • Each row is sorted left → right • Each column is sorted top → bottom Key idea: Start from the top-right corner. Why? • If current value > target → move left • If current value < target → move down This way we eliminate one row or column every step. Time Complexity: O(m + n) Space Complexity: O(1) Big takeaway: Choosing the right starting point can simplify the entire search. Matrix patterns getting stronger day by day. 🔥 Day 43 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Matrix #BinarySearch #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 56/100 | #100DaysOfDSA 🧠⚡ Today’s problem: String Compression A clean in-place array manipulation problem. Core idea: Compress consecutive repeating characters and store the result in the same array. Approach: • Traverse the array using a pointer • Count consecutive occurrences of each character • Write the character to the array • If count > 1 → write its digits one by one • Move forward and repeat Key insight: We don’t need extra space — just carefully manage read & write pointers. Time Complexity: O(n) Space Complexity: O(1) Big takeaway: In-place algorithms require precise pointer control but give optimal space efficiency. Mastering these improves real-world memory optimization skills. 🔥 Day 56 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Strings #TwoPointers #InPlace #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
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
-
-
🔥 Day 356 – Daily DSA Challenge! 🔥 Problem: ⚡ Max Consecutive Ones III Given a binary array nums and an integer k, return the maximum number of consecutive 1s if you can flip at most k zeros. 💡 Key Insight — Sliding Window We maintain a window where the number of zeros is at most k. Core condition: 🧠 How It Works 🔹 Expand the window using right 🔹 Count zeros in the window 🔹 If zeros exceed k → shrink from left 🔹 At every step, window length is a valid answer ⚡ Algorithm Steps ✅ Initialize: left = 0, zero = 0, max = 0 ✅ Traverse with right: If nums[right] == 0 → increment zero While zero > k: shrink window from left Update max length ⚙️ Complexity ✅ Time Complexity: O(n) (each element processed once) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why does this work only for binary arrays? 2️⃣ How would you modify this for longest substring with at most k distinct characters? 3️⃣ What if we want to flip exactly k zeros instead of at most k? #DSA #Day356 #LeetCode #SlidingWindow #TwoPointers #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🔥 Day 353 – Daily DSA Challenge! 🔥 Problem: 🔍 Single Element in a Sorted Array Given a sorted array where every element appears twice except one, find that single element in O(log n) time. 💡 Key Insight — Binary Search on Pairs In a perfect paired array: Pairs start at even indices Pattern breaks at the single element We use binary search to detect where this pattern breaks. 🧠 Core Observation Before the single element: pairs → (even, odd) After the single element: pairs shift → (odd, even) So we normalize mid: if mid is odd → make it even ⚡ Algorithm Logic ✅ Find mid ✅ Make mid even ✅ Compare: If nums[mid] == nums[mid+1] → move right Else → move left This narrows down to the single element. ⚙️ Complexity ✅ Time Complexity: O(log n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why do we force mid to be even? 2️⃣ How would you solve this using XOR in O(n)? 3️⃣ What if elements appear thrice except one? #DSA #Day353 #LeetCode #BinarySearch #Arrays #Optimization #Java #ProblemSolving #KeepCoding
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