🚀 Day 78/100 – #100DaysOfCode Today, I solved “𝐒𝐞𝐭 𝐌𝐚𝐭𝐫𝐢𝐱 𝐙𝐞𝐫𝐨𝐞𝐬” problem on LeetCode 🔍 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Given a matrix, if any element is 0, we need to set its 𝐞𝐧𝐭𝐢𝐫𝐞 𝐫𝐨𝐰 𝐚𝐧𝐝 𝐜𝐨𝐥𝐮𝐦𝐧 𝐭𝐨 0 — in-place without using extra space. 💡 𝐖𝐡𝐚𝐭 𝐈 𝐋𝐞𝐚𝐫𝐧𝐞𝐝: How to optimize space complexity to O(1) Using the first row & column as markers Importance of handling edge cases (first row & first column separately) ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Check if 𝐟𝐢𝐫𝐬𝐭 𝐫𝐨𝐰/𝐜𝐨𝐥𝐮𝐦𝐧 𝐧𝐞𝐞𝐝𝐬 𝐭𝐨 𝐛𝐞 𝐳𝐞𝐫𝐨𝐞𝐝 Use first row & column to mark zeros Update the matrix accordingly Finally update first row & column if needed 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: Time: O(m × n) Space: O(1) #Day78 #100DaysOfCode #DSA #Java #LeetCode #CodingJourney #SoftwareDeveloper #KeepLearning
100DaysOfCode: Solving Set Matrix Zeros in O(1) Space
More Relevant Posts
-
🚀Day 4/100 – LeetCode Journey Today’s problem: 3Sum Closest🔥 Approach (Sorting + Two Pointer)💡 👉 Workflow (step-by-step): 1. Sort the array → helps in using two-pointer efficiently 2. Fix one element (nums[k]) 3. Use two pointers: i = k + 1 (left) j = n - 1 (right) 4. Calculate: sum = nums[k] + nums[i] + nums[j] 5. Compare: If this sum is closer to target than previous → update closestSum ✅ 6. Move pointers: If sum < target → i++ (increase sum) If sum > target → j-- (decrease sum) Repeat until all possibilities are checked. Key Idea: We are not finding exact match, we are finding the closest possible sum to target ⚡ Time Complexity: O(n²) → outer loop + two pointer 🧠 Space Complexity: O(1) → no extra space used Thanks to RAVI KUMAR Sir for guidance! Getting better every day 🚀 #100DaysOfCode #LeetCode #DSA #Java
To view or add a comment, sign in
-
-
🚀 Day #82/100 – 𝐑𝐞𝐦𝐨𝐯𝐞 𝐄𝐥𝐞𝐦𝐞𝐧𝐭 (LeetCode) Today’s problem was Remove Element — a simple yet important question to understand in-place array manipulation. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: We don’t need extra space! We can solve this using a two-pointer approach efficiently. 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? We overwrite unwanted elements and keep only the valid ones at the beginning of the array. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Initialize k = 0 Traverse array: If element ≠ val → place it at index k Increment k Return k (new length) ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(1) #Day82 #100DaysOfCode #Java #DSA #LeetCode #TwoPointers #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟕/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐄𝐪𝐮𝐚𝐥 𝐒𝐮𝐦 𝐆𝐫𝐢𝐝 𝐏𝐚𝐫𝐭𝐢𝐭𝐢𝐨𝐧 𝐈𝐈 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Compute total sum and try all possible horizontal and vertical cuts. If sums are unequal, check whether removing a single element from the larger partition can balance the grid. Use HashMaps to track frequencies of elements in each partition for efficient lookup. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Prefix sum + HashMap frequency tracking + greedy validation. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦 × 𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦 × 𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When exact partitioning isn’t possible, consider whether removing or adjusting a minimal element can achieve balance. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Matrix #HashMap #PrefixSum #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
Day 55/100 🚀 | #100DaysOfDSA Solved LeetCode 16 – 3Sum Closest today. This problem is a variation of 3Sum, but instead of finding triplets equal to a target, we need the sum closest to the target. Approach: • Sorted the array. • Fixed one element and used two pointers (left & right) for the remaining part. • Calculated the current sum and compared it with the target. • Updated the closest value whenever a better (closer) sum was found. • Moved pointers based on whether the sum was less than or greater than the target. • If the sum exactly matched the target, returned immediately. Time Complexity: O(n²) Space Complexity: O(1) Key takeaway: Sometimes problems are not about exact matches — they’re about getting as close as possible, which requires careful comparison and updates. Building on patterns makes solving variations much easier. 💪 #100DaysOfDSA #LeetCode #DSA #Java #Arrays #TwoPointers #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 67 of #LeetCode Challenge ✅ Problem Solved: Check If Two String Arrays are Equivalent 💡 What I learned today: • Learned how to compare two string arrays without joining them • Understood how to traverse multiple strings using pointers • Improved handling of indices across arrays and strings • Realized the importance of edge cases to avoid runtime errors 🧠 Approach: • Used four pointers to track positions in both arrays and strings • Compared characters one by one • Moved to the next string when current string ends • Ensured both arrays are fully traversed at the end 📊 Key Takeaway: Efficient solutions avoid extra space — comparing character by character is better than building new strings 🔥 Consistency + small improvements every day = big progress #Day67 #LeetCode #CodingJourney #DSA #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Some of the hardest problems become manageable once you recognize a repeating pattern. 🚀 Day 105/365 — DSA Challenge Solved: Subarrays with K Different Integers Problem idea: We need to count subarrays that contain exactly k distinct integers. Efficient approach: Use the powerful trick: subarrays with exactly k distinct = subarrays with ≤ k distinct − subarrays with ≤ (k − 1) distinct Steps: 1. Use a sliding window with a hashmap to track frequency of elements 2. Expand window by moving right pointer 3. If distinct count exceeds k, shrink window from the left 4. Count valid subarrays ending at each index 5. Subtract results to get exact count This pattern converts a hard problem into a manageable one. ⏱ Time: O(n) 📦 Space: O(n) Day 105/365 complete. 💻 260 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #HashMap #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on rotating an array to the right by k steps efficiently. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Rotate Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐑𝐞𝐯𝐞𝐫𝐬𝐚𝐥 𝐓𝐞𝐜𝐡𝐧𝐢𝐪𝐮𝐞 • First normalized k using k % n • Reversed the entire array • Reversed the first k elements • Reversed the remaining elements This results in the desired rotation. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Reversal technique is a powerful in-place trick • Breaking a problem into steps simplifies logic • Modulo helps handle large values of k • In-place operations reduce space complexity 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the best solution is not shifting elements — but rearranging them smartly. 63 days consistent 🚀 On to Day 64. #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Day 71/100 – LeetCode Challenge Solved: Remove Duplicates from Sorted List II (Medium) Today’s problem was a great test of Linked List manipulation and handling edge cases efficiently. 🔍 Key Insight: Since the list is sorted, duplicates appear consecutively. Instead of keeping one copy, the challenge is to remove all nodes with duplicate values, leaving only distinct elements. 💡 Approach: Used a dummy node to handle edge cases Applied two-pointer technique (prev & current) Skipped entire duplicate sequences in one pass ⚡ Result: Runtime: 0 ms (Beats 100%) Space Complexity: O(1) 🎯 Key Learning: Handling duplicates in linked lists requires careful pointer updates — especially when the head itself is part of duplicates. Consistency is key 🔥 #Day71 #LeetCode #100DaysOfCode #Java #DataStructures #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 58 of #100DaysOfCode Solved 165. Compare Version Numbers on LeetCode 🔗 🧠 Key Insight: Version strings are split by "." into multiple revisions. We compare each revision numerically (not lexicographically). Example: "1.01" = "1.1" (leading zeros don’t matter) ⚙️ Approach (Split + Compare): 1️⃣ Split both versions using "." 🔹 version1 → s1[] 🔹 version2 → s2[] 2️⃣ Traverse till max length of both arrays 3️⃣ For each index i: 🔹 num1 = i < s1.length ? parseInt(s1[i]) : 0 🔹 num2 = i < s2.length ? parseInt(s2[i]) : 0 4️⃣ Compare: 🔹 if num1 < num2 → return -1 🔹 if num1 > num2 → return 1 5️⃣ If all equal → return 0 ⏱️ Time Complexity: O(n + m) 📦 Space Complexity: O(n + m) (for split arrays) #100DaysOfCode #LeetCode #DSA #Strings #TwoPointers #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 5/100 – LeetCode Journey Today’s problem: Container With Most Water 💧 Approach (Two Pointer) 1. Take two pointers: left = 0 right = n - 1 2. Workflow: Calculate height → min(height[left], height[right]) Width → right - left Area → height × width Update maxWater if needed ✅ 3. Move pointer: Move the smaller height pointer Because larger height won’t increase area if width decreases. 🧠 Key Idea: Area depends on minimum height, so always try to improve the smaller one. ⚡ Time Complexity: O(n) → single traversal 🧠 Space Complexity: O(1) → no extra space Thanks to RAVI KUMAR Sir for guidance! Consistency building day by day 💪 #100DaysOfCode #LeetCode #DSA #Java
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