Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Set Matrix Zeroes in Python
The Set Matrix Zeroes problem requires us to modify a matrix in-place such that if any element is 0, its entire row and column become 0. This is a classic matrix manipulation problem that can be solved efficiently using constant extra space.
Problem Understanding
Given a matrix, if an element is 0, we need to set the entire row and column containing that element to 0. For example ?
| 1 | 0 | 1 |
| 1 | 1 | 1 |
| 1 | 1 | 1 |
The output becomes ?
| 0 | 0 | 0 |
| 1 | 0 | 1 |
| 1 | 0 | 1 |
Optimal Solution Approach
The efficient approach uses the first row and first column as markers to track which rows and columns need to be zeroed. This eliminates the need for extra space ?
Algorithm Steps
1. Check if the first row or first column originally contains zeros
2. Use the first row and column to mark zeros found in the matrix
3. Set matrix elements to zero based on the markers
4. Handle the first row and column separately
Implementation
def setZeroes(matrix):
if not matrix or not matrix[0]:
return matrix
rows = len(matrix)
cols = len(matrix[0])
# Check if first row and first column have zeros
first_row_zero = any(matrix[0][j] == 0 for j in range(cols))
first_col_zero = any(matrix[i][0] == 0 for i in range(rows))
# Use first row and column as markers
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][j] == 0:
matrix[i][0] = 0 # Mark row
matrix[0][j] = 0 # Mark column
# Set zeros based on markers
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
# Handle first row and column
if first_row_zero:
for j in range(cols):
matrix[0][j] = 0
if first_col_zero:
for i in range(rows):
matrix[i][0] = 0
return matrix
# Test the function
matrix = [[1, 0, 1], [1, 1, 1], [1, 1, 1]]
print("Original matrix:", matrix)
result = setZeroes(matrix)
print("After setting zeroes:", result)
Original matrix: [[1, 0, 1], [1, 1, 1], [1, 1, 1]] After setting zeroes: [[0, 0, 0], [1, 0, 1], [1, 0, 1]]
Another Example
def setZeroes(matrix):
if not matrix or not matrix[0]:
return matrix
rows = len(matrix)
cols = len(matrix[0])
first_row_zero = any(matrix[0][j] == 0 for j in range(cols))
first_col_zero = any(matrix[i][0] == 0 for i in range(rows))
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[0][j] = 0
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if first_row_zero:
for j in range(cols):
matrix[0][j] = 0
if first_col_zero:
for i in range(rows):
matrix[i][0] = 0
return matrix
# Test with multiple zeros
matrix2 = [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]]
print("Original matrix:", matrix2)
result2 = setZeroes(matrix2)
print("After setting zeroes:", result2)
Original matrix: [[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]] After setting zeroes: [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
Time and Space Complexity
Time Complexity: O(m × n) where m and n are the matrix dimensions
Space Complexity: O(1) as we use only constant extra space
Conclusion
The optimal solution uses the first row and column as markers to track zeros, achieving O(1) space complexity. This in-place approach efficiently handles the matrix transformation without requiring additional storage.
