Solution: Set Matrix Zeros
Let's solve the Set Matrix Zeros problem using the Matrix Transformations pattern.
Statement#
Given a matrix, mat, if any element within the matrix is zero, set that row and column to zero.
Constraints:
-
mat.row,mat.col -
mat[i][j]
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
The naive approach to solve this problem is by taking an extra matrix, mat2. First, copy all the elements of the given mat to mat2. Then, while traversing mat, whenever we encounter 0, we will make the entire row and column of the mat2 to 0. In the end, again, copy all the elements of mat2 to mat. This solution has high time and space complexity, which is and , respectively. Let’s see the optimized approach.
Optimized approach using the matrices pattern#
The first step is to get the number of rows and columns of the mat. Then we set two boolean flags, fcol and frow, to indicate any presence of 0 in the first row and first column, respectively, and set both flags to FALSE. Now, we check the first column and first row and set the frow and fcol to TRUE if we find any 0 in the first row and the first column, respectively. This is because at this stage, we will not set the entire first row or column to 0 as it can make the following rows and columns 0.
Now, we check all the elements row-wise (by ignoring the first row and first column), and if a 0 is found, we set the first element in that row and column to 0. Now, we set 0 in rows and columns where required by following rules:
- Check every row’s first element, starting from the second row, and if it is
0, set all values in that row to0. - Check every column’s first element, starting from the second column, and if it is
0, set all values in that row to0.
Now, we check the first row and first column. If the values of frow and fcol are TRUE, it indicates any 0 in the first row and first column, respectively. We will set 0 in the corresponding row and column.
In the end, we return the mat.
Let’s look at the following illustration to get a better understanding of the solution:
1 of 14
2 of 14
3 of 14
4 of 14
5 of 14
6 of 14
7 of 14
8 of 14
9 of 14
10 of 14
11 of 14
12 of 14
13 of 14
14 of 14
Let’s have a look at the code for the algorithm we just discussed.
Solution summary#
- Calculate the number of rows and columns of the
mat. - Set
fcolandfrowto FALSE. - Check the first column and first row. If any element from the first row or first column is
0, setfroworfcolto TRUE, respectively. - Check all the elements row-wise by ignoring the first row and first column. If a
0is found, we set the first element in that row and column to0. - Check every row’s first element, starting from the second row, and if it is
0, set all values in that row to0. - Check every column’s first element, starting from the second column, and if it’s
0, set all values in that row to0. - If
frowis TRUE, set0in the first row. - If
fcolis TRUE, set0in the first column. - Return
mat.
Time complexity#
The time complexity of this algorithm is , where and are the dimensions of the matrix.
Space complexity#
The space complexity is because we use no extra memory.
Set Matrix Zeros
Rotate Image