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.
For example:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
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
Can you devise a constant space 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:
The brute force way to solve this is to check every spot in the matrix. If we find a zero, we need to remember its location so we can change its row and column later. We will then go back through and change the rows and columns containing the zeros we remembered.
Here's how the algorithm would work step-by-step:
def set_matrix_zeroes_brute_force(matrix):
rows_with_zero = len(matrix)
cols_with_zero = len(matrix[0])
zero_positions = []
for row_index in range(rows_with_zero):
for col_index in range(cols_with_zero):
if matrix[row_index][col_index] == 0:
zero_positions.append((row_index, col_index))
# Iterate through the stored zero positions.
for zero_row, zero_col in zero_positions:
# Zero out the entire row
for col_index in range(cols_with_zero):
matrix[zero_row][col_index] = 0
# Zero out the entire column
for row_index in range(rows_with_zero):
matrix[row_index][zero_col] = 0
return matrix
The problem requires us to modify a matrix so that if a cell is zero, its entire row and column become zero. The optimal approach avoids using extra space by cleverly using the first row and column of the matrix itself to record which rows and columns need to be zeroed out.
Here's how the algorithm would work step-by-step:
def set_matrix_zeroes(matrix):
first_row_has_zero = False
first_column_has_zero = False
number_of_rows = len(matrix)
number_of_columns = len(matrix[0])
for column_index in range(number_of_columns):
if matrix[0][column_index] == 0:
first_row_has_zero = True
break
for row_index in range(number_of_rows):
if matrix[row_index][0] == 0:
first_column_has_zero = True
break
# Mark rows and columns that need to be zeroed
# using the first row and first column.
for row_index in range(1, number_of_rows):
for column_index in range(1, number_of_columns):
if matrix[row_index][column_index] == 0:
matrix[0][column_index] = 0
matrix[row_index][0] = 0
# Use the marks in the first row and column
# to set the inner matrix elements to zero.
for row_index in range(1, number_of_rows):
for column_index in range(1, number_of_columns):
if matrix[0][column_index] == 0 or matrix[row_index][0] == 0:
matrix[row_index][column_index] = 0
# Zero out the first row if needed.
if first_row_has_zero:
for column_index in range(number_of_columns):
matrix[0][column_index] = 0
# Zero out the first column if needed.
if first_column_has_zero:
for row_index in range(number_of_rows):
matrix[row_index][0] = 0
Case | How to Handle |
---|---|
Null or empty matrix (matrix == null or matrix.length == 0 or matrix[0].length == 0) | Return immediately as there's nothing to process; no modification needed. |
Matrix with one row or one column | The in-place modification logic still works correctly for single rows or columns because we iterate through all cells. |
Matrix with all zeros | All rows and columns will be zeroed out correctly by identifying the initial zero cells. |
Matrix with no zeros | The matrix remains unchanged as no zero elements are found, thus no rows or columns are modified. |
Large matrix (m and n are large, approaching memory limits) | The space-optimized solution using the first row and column as flags will scale better than using extra memory for row/col sets but still needs to be considered for large datasets; potential for memory issues remains. |
Matrix containing very large or very small integer values (close to Integer.MAX_VALUE or Integer.MIN_VALUE) | Integer overflow is not a relevant concern for this algorithm as it only deals with equality (checking for zeros) and not arithmetic operations on the values themselves. |
First cell in the matrix is zero | Special attention is required because the first row and column are used as flags, so we need to use additional variables to track their initial zero status before zeroing out. |
Sparse matrix (few non-zero elements) | The algorithm will still perform correctly; it iterates over all elements regardless of their values and sets row/col flags appropriately for zeros. |