data_structures.arrays.set_matrix_zeroes¶
If an element in an m x n matrix is 0, set its entire row and column to 0.
Explanation: We use the first row and first column as markers to track which rows and columns should be zeroed, avoiding extra space usage (O(1) space complexity).
References: https://leetcode.com/problems/set-matrix-zeroes/
Doctest: >>> matrix = [ … [1, 1, 1], … [1, 0, 1], … [1, 1, 1] … ] >>> set_matrix_zeroes(matrix) >>> matrix [[1, 0, 1], [0, 0, 0], [1, 0, 1]] >>> matrix = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]] >>> set_matrix_zeroes(matrix) >>> matrix [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
Functions¶
|
Modify the matrix in-place such that if an element is 0, |
Module Contents¶
- data_structures.arrays.set_matrix_zeroes.set_matrix_zeroes(matrix: list[list[int]]) None¶
Modify the matrix in-place such that if an element is 0, its entire row and column are set to 0.
- Parameters:
matrix – 2D list of integers
- Returns:
None (modifies matrix in-place)
Time Complexity: O(m * n) Space Complexity: O(1)