--- title: '73. Set Matrix Zeroes' description: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's icon: dot topics: - { name: "Array", slug: "array" } - { name: "Hash Table", slug: "hash-table" } - { name: "Matrix", slug: "matrix" } video: "T41rL0L3Pnw" issue: "https://github.com/prdlk/leetcode/issues/93" companies: - { name: "Bloomberg", period: "Three Months", frequency: 54 } - { name: "Amazon", period: "Three Months", frequency: 48.7 } - { name: "Meta", period: "Three Months", frequency: 43.3 } - { name: "Microsoft", period: "Three Months", frequency: 33.4 } - { name: "Google", period: "Three Months", frequency: 32.7 } - { name: "tcs", period: "Six Months", frequency: 43.5 } sidebar: label: 'Set Matrix Zeroes' badge: 'Medium' --- ::::warning You must do it in place. :::: ### Example 1: - Input: `matrix = [[1,1,1],[1,0,1],[1,1,1]]` - Output: `[[1,0,1],[0,0,0],[1,0,1]]` ### Example 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]]` ### Constraints: - `m == matrix.length` - `n == matrix[0].length` - `1 <= m, n <= 200` - `-2^31 <= matrix[i][j] <= 2^31 - 1` - A straightforward solution using O(mn) space is probably a bad idea. - A simple improvement uses O(m + n) space, but still not the best solution. - Could you devise a constant space solution? ## Approach ```mermaid flowchart TD S(["setZeroes(matrix)"]) --> I["m = len(matrix), n = len(matrix[0])"] I --> Z["row0_zero = any(matrix[0][c] == 0), col0_zero = any(matrix[r][0] == 0) — remember before they become markers"] Z --> F{"more r in range(1, m)?"} F -- yes --> G{"more c in range(1, n)?"} G -- yes --> Q{"matrix[r][c] == 0?"} Q -- yes --> M["matrix[r][0] = 0, matrix[0][c] = 0 — row 0 and column 0 are the marker lists"] M --> G Q -- no --> G G -- no --> F F -- no --> H{"more r in range(1, m)?"} H -- yes --> J{"more c in range(1, n)?"} J -- yes --> K{"matrix[r][0] == 0 or matrix[0][c] == 0?"} K -- yes --> W["matrix[r][c] = 0"] W --> J K -- no --> J J -- no --> H H -- no --> A{"row0_zero?"} A -- yes --> B["matrix[0][c] = 0 for c in range(n)"] B --> C{"col0_zero?"} A -- no --> C C -- yes --> D["matrix[r][0] = 0 for r in range(m)"] D --> E(["return None — zeroed in place"]) C -- no --> E ``` ## Solution ```py class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ m, n = len(matrix), len(matrix[0]) # 1. remember if row 0 or column 0 have their own zeros row0_zero = any(matrix[0][c] == 0 for c in range(n)) col0_zero = any(matrix[r][0] == 0 for r in range(m)) # 2. use row 0 and column 0 as marker lists for r in range(1, m): for c in range(1, n): if matrix[r][c] == 0: matrix[r][0] = 0 matrix[0][c] = 0 # 3. zero the inner cells from the markers for r in range(1, m): for c in range(1, n): if matrix[r][0] == 0 or matrix[0][c] == 0: matrix[r][c] = 0 # 4. handle row 0 and column 0 last if row0_zero: for c in range(n): matrix[0][c] = 0 if col0_zero: for r in range(m): matrix[r][0] = 0 ```