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:

  • 11 \le mat.row, mat.col 20\le 20
  • 231-2^{31} \le mat[i][j] 231\le 2^{31}

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 O((mn)×(m+n))O((mn) \times (m+n)) and O(mn)O(mn), 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 to 0.
  • Check every column’s first element, starting from the second column, and if it is 0, set all values in that row to 0.

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:

Created with Fabric.js 3.6.6 3 5 2 0 1 0 4 0 7 3 2 4 rows = 3cols = 4frow = FALSEfcol = FALSE Initialize frow and fcol to FALSE.

1 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 5 2 0 1 0 4 0 7 3 2 4 Set frow to TRUE because there is a 0 in the first row.Set fcol to FALSE becuse there is no 0 in the first column.

2 of 14

Created with Fabric.js 3.6.6 3 0 2 0 0 0 4 0 7 3 2 4 rows = 3cols = 4frow = TRUEfcol = FALSE Scan the second row, skipping the first elementElement is 0, set first element in the row and the column to 0.

3 of 14

Created with Fabric.js 3.6.6 3 0 2 0 0 0 4 0 7 3 2 4 rows = 3cols = 4frow = TRUEfcol = FALSE Check the next element in the second rowThe element is 4. Do nothing and move to the next element.

4 of 14

Created with Fabric.js 3.6.6 3 0 2 0 0 0 4 0 7 3 2 4 rows = 3cols = 4frow = TRUEfcol = FALSE Check the next element in the second rowThe element is 0. Set the first element in the row and the column to 0.

5 of 14

Created with Fabric.js 3.6.6 3 0 2 0 0 0 4 0 7 3 2 4 rows = 3cols = 4frow = TRUEfcol = FALSE Scan the third row, skipping the first elementThe element is 3. Do nothing and move to the next element

6 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 4 0 7 3 2 4 Check the next element in the third rowThe element is 2. Do nothing and move to the next element

7 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 4 0 7 3 2 4 Check the next element in the third rowThe element is 4. Do nothing and stop.

8 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 0 0 7 3 2 4 The first element of the second row is 0. Set the complete row to 0.

9 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 0 0 7 3 2 4 The first element of the third row is not 0. Do nothing.

10 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 0 0 7 0 2 4 The first element of the second column is 0. Set the complete column to 0.

11 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 0 0 7 0 2 4 The first element of the third column is 2. Do nothing.

12 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 3 0 2 0 0 0 0 0 7 0 2 0 The first element of the fourth column is 0. Set the complete column to 0.

13 of 14

Created with Fabric.js 3.6.6 rows = 3cols = 4frow = TRUEfcol = FALSE 0 0 0 0 0 0 0 0 7 0 2 0 Since frow=TRUE, set the first row's elements to 0.

14 of 14

Let’s have a look at the code for the algorithm we just discussed.

Set Matrix Zeros

Solution summary#

  1. Calculate the number of rows and columns of the mat.
  2. Set fcol and frow to FALSE.
  3. Check the first column and first row. If any element from the first row or first column is 0, set frow or fcol to TRUE, respectively.
  4. Check all the elements row-wise by ignoring the first row and first column. If a 0 is found, we set the first element in that row and column to 0.
  5. Check every row’s first element, starting from the second row, and if it is 0, set all values in that row to 0.
  6. Check every column’s first element, starting from the second column, and if it’s 0, set all values in that row to 0.
  7. If frow is TRUE, set 0 in the first row.
  8. If fcol is TRUE, set 0 in the first column.
  9. Return mat.

Time complexity#

The time complexity of this algorithm is O(m×n)O(m \times n), where mm and nn are the dimensions of the matrix.

Space complexity#

The space complexity is O(1)O(1) because we use no extra memory.

Set Matrix Zeros

Rotate Image