Optimized Solution To Set Matrix Zeroes In O(1) Space Complexity
Optimized solution to set zeros in a matrix. Use two boolean vars to track first row & col with zero, then iterate through rest of matrix to mark rows & cols with zero.
Intuition In a naive solution, we can keep track of all the rows and columns to be made zero. Approach In the naive solution, we can use hash sets or sets to keep track of rows and columns to make zero using one iteration. In the second phase, we can iterate through our sets to set the saved rows and columns to zero. Space complexity for Naive Solution: O(M+N) Naive solution code: class Solution { public void setRowZero(int[][] matrix, int row){ for(int i = 0;i<matrix[0].length;i++){ matrix[row][i] = 0; } } public void setColZero(int[][] matri...