leetcode

Set Matrix Zeroes - Link

Question Description

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.


Constraints


Approach

Use the first row and first column as markers to record which rows/columns should be zeroed. Track separately if the first row or first column need to be zeroed to avoid losing marker info. In the second pass, set entire rows/columns to zero based on these markers.

Why this approach works:

Alternative approaches considered:


Dry Run

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

Step-by-step execution:

Final Answer = [[1,0,1],[0,0,0],[1,0,1]]


Solution

class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;

        boolean r0F = false;
        boolean c0F = false;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && matrix[i][j] == 0) r0F = true;
                if (j == 0 && matrix[i][j] == 0) c0F = true;
                if (matrix[i][j] == 0) {
                    matrix[0][j] = 0;
                    matrix[i][0] = 0;
                }
            }
        }

        for (int i = 1; i < n; i++) {
            if (matrix[0][i] == 0) {
                for (int j = 0; j < m; j++) {
                    matrix[j][i] = 0;
                }
            }
        }

        for (int i = 1; i < m; i++) {
            if (matrix[i][0] == 0) {
                for (int j = 0; j < n; j++) {
                    matrix[i][j] = 0;
                }
            }
        }

        if (r0F) {
            for (int i = 0; i < n; i++) {
                matrix[0][i] = 0;
            }
        }

        if (c0F) {
            for (int i = 0; i < m; i++) {
                matrix[i][0] = 0;
            }
        }
    }
}

Time and Space Complexity