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.
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.lengthn == matrix[0].length1 <= m, n <= 200-231 <= matrix[i][j] <= 231 - 1Follow up:
O(mn) space is probably a bad idea.O(m + n) space, but still not the best solution.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine looking at a grid filled with numbers and spotting a zero. The brute force strategy involves searching the entire grid for zeros, and whenever one is found, safely marking its entire row and column for erasure without disrupting the rest of the search.
Here's how the algorithm would work step-by-step:
def set_zeroes(matrix):
row_count = len(matrix)
column_count = len(matrix[0])
marker_value = float('inf')
# We scan the entire matrix first to locate original zeros without false triggers for row_index in range(row_count):
for column_index in range(column_count):
if matrix[row_index][column_index] == 0:
for target_column in range(column_count):
if matrix[row_index][target_column] != 0:
matrix[row_index][target_column] = marker_value
for target_row in range(row_count):
if matrix[target_row][column_index] != 0:
matrix[target_row][column_index] = marker_value
# We use a temporary marker to avoid confusing subsequent zero searches in the grid for row_index in range(row_count):
for column_index in range(column_count):
if matrix[row_index][column_index] == marker_value:
matrix[row_index][column_index] = 0Instead of using extra space to remember which spots need to be wiped out, the optimal approach uses the edges of the grid itself as a memory board. By checking the outer borders first, we can cleverly track our targets without needing any extra materials.
Here's how the algorithm would work step-by-step:
def set_zeroes(matrix_grid):
if not matrix_grid or not matrix_grid[0]:
return
row_count = len(matrix_grid)
column_count = len(matrix_grid[0])
top_row_has_zero = False
left_col_has_zero = False
for column_index in range(column_count):
if matrix_grid[0][column_index] == 0:
top_row_has_zero = True
break
for row_index in range(row_count):
if matrix_grid[row_index][0] == 0:
left_col_has_zero = True
break
# We use edges as markers to avoid extra space complexity.
for row_index in range(1, row_count):
for column_index in range(1, column_count):
if matrix_grid[row_index][column_index] == 0:
matrix_grid[row_index][0] = 0
matrix_grid[0][column_index] = 0
# Inner cells are updated based on markers found on the borders.
for row_index in range(1, row_count):
for column_index in range(1, column_count):
if matrix_grid[row_index][0] == 0 or matrix_grid[0][column_index] == 0:
matrix_grid[row_index][column_index] = 0
# The first row and column are updated independently based on initial checks.
if top_row_has_zero:
for column_index in range(column_count):
matrix_grid[0][column_index] = 0
if left_col_has_zero:
for row_index in range(row_count):
matrix_grid[row_index][0] = 0| Case | How to Handle |
|---|---|
| Empty matrix or matrix with zero rows or columns | Check for null or empty inputs at the very beginning and return immediately without performing operations. |
| Matrix of size 1x1 containing either zero or a non-zero value | The algorithm should bypass row and column marker logic since a single element cannot trigger zeroing of external rows and columns. |
| Matrix where the top-left element itself is zero | Use a dedicated boolean flag to track whether the first column should be zeroed independently from the first row marker. |
| Matrix with all elements already set to zero | The algorithm naturally processes and maintains all zeros without redundant modifications or infinite loops. |
| Matrix with no zeros present anywhere | The initial scan finds no zeros, leaving the matrix completely unmodified as required. |
| Matrix containing negative numbers and extreme boundary values | Since zero is the only sentinel trigger, negative numbers are treated as standard non-zero values and do not interfere with the logic. |
| Very large matrices approaching maximum constraints | The in-place constant space O(1) approach prevents memory limit exceeded errors by reusing the first row and column as markers. |
| Zeros located exclusively in the first row or first column | Separate tracking variables ensure the first row and first column state are preserved correctly before being zeroed out last. |