Explorer
Data Structures & Algorithms

Set entire row and column to 0 if an element in matrix is 0.

Problem Statement

Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it in place and return the matrix.

Examples

Input: matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]

Output: [[1, 0, 1], [0, 0, 0], [1, 0, 1]]

Explanation: Any row or column containing a 0 has all its elements set to 0.

Input: matrix = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]]

Output: [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]

Explanation: Any row or column containing a 0 has all its elements set to 0.

Complexity

Time Complexity: O(M * N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Using an O(M*N) or O(M+N) extra space is straightforward. Can you do it in O(1) space? šŸ’” Hint 2: Use the first row and first column of the matrix itself as markers. šŸ’” Hint 3: Use a variable col0 to track if the very first column needs to be zeroed out.

Editorial & Approach

Problem Overview & Intuition

Instead of allocating additional memory for row and column markers, use matrix[i][0] and matrix[0][j] to record zeroes. Use a single boolean flag for the first column.

Step-by-Step Approach

  1. Scan the matrix and record zero occurrences in row 0 and column 0.
  2. Use col0 to record if column 0 contains any zeroes.
  3. Iterate backwards from bottom-right to update cells based on row and column markers.
  4. Return matrix.

Optimal Implementation (JavaScript)

function setZeroes(matrix) {
  let col0 = 1;
  const m = matrix.length, n = matrix[0].length;
  for (let i = 0; i < m; i++) {
    if (matrix[i][0] === 0) col0 = 0;
    for (let j = 1; j < n; j++) {
      if (matrix[i][j] === 0) {
        matrix[i][0] = 0;
        matrix[0][j] = 0;
      }
    }
  }
  for (let i = m - 1; i >= 0; i--) {
    for (let j = n - 1; j >= 1; j--) {
      if (matrix[i][0] === 0 || matrix[0][j] === 0) {
        matrix[i][j] = 0;
      }
    }
    if (col0 === 0) matrix[i][0] = 0;
  }
  return matrix;
}

Complexity Analysis

Time Complexity O(M * N) — two full passes over the matrix.
Space Complexity O(1) — in-place modification.

Edge Cases & Corner Traps Handled

  • Single cell matrix [0] or [1].
  • Single row or single column matrix.
  • Matrix with no zeroes.

Set Matrix Zeroes

Medium
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it in place and return the matrix.
Example Scenarios
1Example 1
Input: matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
Output: [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
Explanation:

Any row or column containing a 0 has all its elements set to 0.

2Example 2
Input: matrix = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]]
Output: [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
Explanation:

Any row or column containing a 0 has all its elements set to 0.

Editor
Loading Editor...
matrix =
[[1, 1, 1], [1, 0, 1], [1, 1, 1]]
Output:Click "Run" above to execute and verify your code here.
[[1,0,1],[0,0,0],[1,0,1]]