Browse curriculum

Set Matrix Zeroes

Spread every 0 in a matrix across its full row and column, in place.

Problem Understanding

Wherever an m x n integer matrix holds a 0, clear that cell's whole row and whole column to 0, modifying the matrix in-place rather than building a copy.

Strategy (O(1) Space)

We can use the first row and first column as markers.

  1. Check if first row/col originally have zeros. Store in flags.
  2. Iterate through the rest of the matrix. If matrix[i][j] == 0, mark matrix[i][0] and matrix[0][j] as 0.
  3. Iterate again to set zeros based on markers.
  4. Handle first row/col separately using flags.
  • Use matrix[i][0] to store row status.

  • Use matrix[0][j] to store col status.

Interactive Visualization

Watch how we use the first row and column as makers to avoid using O(MN) extra space.

Dry Run

Matrix: [[1,1,1],[1,0,1],[1,1,1]]

Step Action State
1 Scan Row 0/Col 0 No zeroes found. Flags=False.
2 Scan Inner matrix[1][1] is 0. Mark [1][0]=0, [0][1]=0.
3 Apply Marks [1][0] is 0 -> Set Row 1 to 0. [0][1] is 0 -> Set Col 1 to 0.
4 Apply Flags Flags are False. Skip.

Result: [[1,0,1],[0,0,0],[1,0,1]]

Edge Cases & Common Mistakes

Watch out for:

  • First Row/Col Overwrite: If you don't use separate flags (firstRowZero), you might wipe out the markers needed for the rest of the matrix.

  • Order of Operations: Process inner matrix first, THEN the first row/col based on flags.

Solution

O(M * N) Time, O(1) Space.

Set Matrix Zeroes Code
1var setZeroes = function(matrix) {
2 const m = matrix.length, n = matrix[0].length;
3 let firstRowZero = false, firstColZero = false;
4
5 for(let i=0; i<m; i++) if(matrix[i][0] === 0) firstColZero = true;
6 for(let j=0; j<n; j++) if(matrix[0][j] === 0) firstRowZero = true;
7
8 for(let i=1; i<m; i++) {
9 for(let j=1; j<n; j++) {
10 if(matrix[i][j] === 0) {
11 matrix[i][0] = 0;
12 matrix[0][j] = 0;
13 }
14 }
15 }
16
17 for(let i=1; i<m; i++) {
18 for(let j=1; j<n; j++) {
19 if(matrix[i][0] === 0 || matrix[0][j] === 0) {
20 matrix[i][j] = 0;
21 }
22 }
23 }
24
25 if(firstColZero) for(let i=0; i<m; i++) matrix[i][0] = 0;
26 if(firstRowZero) for(let j=0; j<n; j++) matrix[0][j] = 0;
27};

The approach, step by step

  1. Record the flags

    Check whether column 0 and row 0 contain a zero and store the answers in two booleans.

  2. Mark rows and columns

    For every inner cell that is zero, set matrix[i][0] and matrix[0][j] to zero.

  3. Clear marked cells

    For every inner cell, set it to zero if its row marker or column marker is zero.

  4. Clear row 0 and column 0

    Finally, zero column 0 and row 0 if their flags were set.

Frequently asked questions

How do you solve Set Matrix Zeroes with O(1) extra space?

Use the first row and first column as markers. When an inner cell is zero, set matrix[i][0] and matrix[0][j] to zero; a second pass then clears every inner cell whose row marker or column marker is zero.


Why do row 0 and column 0 need separate flags?

They are overwritten when they are used as markers, so two booleans record whether they held a zero before that happened. They are cleared last, after the inner cells have been set from the markers.


What is the time and space complexity of Set Matrix Zeroes?

O(m × n) time over a fixed number of passes, and O(1) extra space: two booleans, with the markers stored in the matrix itself.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description