Notes

Personal notes on various topics

View on GitHub

Intuition

To set matrix zeroes in-place with constant space, we can use the first row and first column as markers to indicate which rows and columns should be zeroed. Special care is needed for the first row and column themselves.

Approach

Complexity

Code

from typing import List

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        is_col = False
        m = len(matrix)
        n = len(matrix[0])

        for i in range(m):
            if matrix[i][0] == 0:
                is_col = True

            for j in range(1, n):
                if matrix[i][j] == 0:
                    matrix[i][0] = 0
                    matrix[0][j] = 0

        for i in range(1, m):
            for j in range(1, n):
                if not matrix[i][0] or not matrix[0][j]:
                    matrix[i][j] = 0

        if matrix[0][0] == 0:
            for j in range(n):
                matrix[0][j] = 0

        if is_col:
            for i in range(m):
                matrix[i][0] = 0

Back to Problem Statement