Understand Convert Matrix Rows and Columns to Zeroes Problem

Problem Name: Convert Matrix Rows and Columns to Zeroes
Problem Description:

Problem Statement

Given an m x n matrix (a grid) of integers, modify the matrix in-place such that all elements in any row or column that contains a zero are set to zero.

Approach

The approach involves three steps:

  1. Identify Rows and Columns with Zeros: Traverse the entire matrix to find rows and columns that contain at least one zero.
  2. Mark Rows and Columns for Zeroing: Use separate arrays to mark which rows and columns need to be set to zero based on the previous step.
  3. Set Elements to Zero: Iterate through the marked rows and columns and set all their elements to zero.

Solution Code (Conceptual)

function setZeroes(matrix) {
    let m = matrix.length;
    if (m === 0) return;
    let n = matrix[0].length;

    // Arrays to mark which rows and columns need to be set to zero
    let zeroesRow = new Array(m).fill(false);
    let zeroesCol = new Array(n).fill(false);

    // Step 1: Identify rows and columns containing zeros
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (matrix[i][j] === 0) {
                zeroesRow[i] = true;
                zeroesCol[j] = true;
            }
        }
    }

    // Step 2: Set the entire row or column to zero
    for (let i = 0; i < m; i++) {
        if (zeroesRow[i]) {
            matrix[i].fill(0);
        }
    }

    for (let j = 0; j < n; j++) {
        if (zeroesCol[j]) {
            // Ensure all elements in this column are set to zero
            for (let i = 0; i < m; i++) {
                matrix[i][j] = 0;
            }
        }
    }

    return matrix;
}

Solution Explanation

  1. Identify Rows and Columns with Zeros: We iterate through each element of the matrix. If we encounter a zero, we mark its corresponding row and column in zeroesRow and zeroesCol, respectively.
  2. Mark Rows and Columns for Zeroing: After identifying all rows and columns containing zeros, we set those marked rows to all zeros using fill(0). We then iterate through each marked column and set each element in that column to zero.
  3. Edge Cases: This approach handles cases where the matrix contains only one row or one column correctly by ensuring both steps (row marking and column iteration) are performed, thus avoiding any missed elements due to overwriting.

Space Complexity

  • The space complexity of this solution is O(m + n), where m is the number of rows and n is the number of columns.
  • This is because we use two additional arrays (zeroesRow and zeroesCol) to keep track of which rows and columns need to be zeroed out.

Example Walkthrough

Consider a 3x3 matrix:

1 2 0
4 5 6
7 8 9
  • Step 1: Identify zeros at position (0,2). Mark row 0 and column 2.
  • Step 2: Set all elements in row 0 to zero. The matrix becomes:
0 0 0
4 5 6
7 8 9
  • Then, set all elements in column 2 to zero. The final matrix is:
0 0 0
4 5 0
7 8 0

Key Takeaways

  • This approach efficiently modifies the matrix in place with a time complexity of O(m * n) and minimal additional space, making it suitable for large matrices while adhering to constraints.
Category:
  • Arrays
  • Heaps & Hashing
  • Striver's SDE sheet
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/set-matrix-zeroes/

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

Main Function is not defined.

Helper Function:

INPUT: [[1 2 0][ 4 5 6][ 7 8 9]]

OUTPUT: [[0 0 0][ 4 5 6 ][7 8 9]]

public static void setZeroes(int[][] matrix) {

int m = matrix.length;

int n = matrix[0].length;

boolean firstRowZero = false;

boolean firstColZero = false;

for(int i =0; i < m; i++) {

if( matrix[i][0] == 0) {

firstColZero = true;

break;

}//If End

}//Loop End

for (int j = 0; j < n; j++) {

if (matrix[0][j] == 0) {

firstRowZero = true;

break;

}//If End

}//Loop End

for (int i = 1; i < m; i++) {

for (int j = 1; j < n; j++) {

if (matrix[i][j] == 0) {

matrix[i][0] = 0;

matrix[0][j] = 0;

}//If End

}//Loop End

}//Loop End

for (int i = 1; i < m; i++) {

if (matrix[i][0] == 0) {

Arrays.fill(matrix[i], 0);

}//If End

}//Loop End

for (int j = 1; j < n; j++) {

if (matrix[0][j] == 0) {

for (int i = 0; i < m; i++) {

matrix[i][j] = 0;

}//Loop End

}//If End

}//Loop End

if (firstRowZero) {

Arrays.fill(matrix[0], 0);

}//If End

if (firstColZero) {

for (int i = 0; i < m; i++) {

matrix[i][0] = 0;

}//Loop End

}//If End

}//function end

Utility Functions and Global variables:

Utility Function is not required.