100DaysOfCode: Solving Set Matrix Zeros in O(1) Space

🚀 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

To view or add a comment, sign in

Explore content categories