27. Set Matrix Zeroes
β GFG solution to the Set Matrix Zeroes problem: modify matrix to set entire rows and columns to zero when encountering a zero element using optimal space approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a 2D matrix mat[][]
of size n x m
. The task is to modify the matrix such that if mat[i][j]
is 0, all the elements in the i-th row and j-th column are set to 0.
The challenge is to solve this in-place without using extra space proportional to the matrix size.
π Examples
Example 1
Input: mat = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
Output: [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
Explanation: mat[1][1] = 0, so all elements in row 1 and column 1 are updated to zeroes.
Example 2
Input: mat = [[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]]
Explanation: mat[0][0] and mat[0][3] are 0s, so all elements in row 0, column 0 and column 3 are updated to zeroes.
π Constraints
$1 \le n, m \le 500$
$-2^{31} \le \text{mat}[i][j] \le 2^{31} - 1$
β
My Approach
The optimal approach uses the first row and first column as markers to track which rows and columns should be zeroed, achieving O(1) space complexity:
In-Place Flagging with Boundary Tracking
Track Boundary States:
Use boolean flags
firstRow
andfirstCol
to remember if the first row or first column originally contained zeros.This prevents confusion when using them as markers.
Mark Using Matrix Itself:
Iterate through the matrix starting from
(1,1)
.When finding a zero at
mat[i][j]
, markmat[i][0] = 0
andmat[0][j] = 0
.This uses the first row and column as a "marking system".
Apply Zeros Based on Markers:
For each cell
mat[i][j]
(excluding first row/column), if eithermat[i][0]
ormat[0][j]
is zero, setmat[i][j] = 0
.
Handle Boundaries:
If
firstRow
was true, zero out the entire first row.If
firstCol
was true, zero out the entire first column.
Why This Works:
We use the matrix's own space to store marking information.
By handling boundaries separately, we avoid overwriting critical marker data.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(nΓm), where n and m are the dimensions of the matrix. We traverse the matrix a constant number of times (3-4 passes).
Expected Auxiliary Space Complexity: O(1), as we only use two boolean variables for boundary tracking, regardless of matrix size.
π§βπ» Code (C++)
class Solution {
public:
void setMatrixZeroes(vector<vector<int>>& mat) {
int n = mat.size(), m = mat[0].size();
bool firstRow = false, firstCol = false;
for (int i = 0; i < n; ++i) firstCol |= mat[i][0] == 0;
for (int j = 0; j < m; ++j) firstRow |= mat[0][j] == 0;
for (int i = 1; i < n; ++i)
for (int j = 1; j < m; ++j)
if (mat[i][j] == 0)
mat[i][0] = mat[0][j] = 0;
for (int i = 1; i < n; ++i)
for (int j = 1; j < m; ++j)
if (!mat[i][0] || !mat[0][j])
mat[i][j] = 0;
if (firstRow) fill(begin(mat[0]), end(mat[0]), 0);
if (firstCol) for (int i = 0; i < n; ++i) mat[i][0] = 0;
}
};
π§βπ» Code (Java)
class Solution {
public void setMatrixZeroes(int[][] mat) {
int n = mat.length, m = mat[0].length;
boolean row0 = false, col0 = false;
for (int i = 0; i < n; i++) if (mat[i][0] == 0) col0 = true;
for (int j = 0; j < m; j++) if (mat[0][j] == 0) row0 = true;
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
if (mat[i][j] == 0)
mat[i][0] = mat[0][j] = 0;
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
if (mat[i][0] == 0 || mat[0][j] == 0)
mat[i][j] = 0;
if (row0) Arrays.fill(mat[0], 0);
if (col0) for (int i = 0; i < n; i++) mat[i][0] = 0;
}
}
π Code (Python)
class Solution:
def setMatrixZeroes(self, mat):
n, m = len(mat), len(mat[0])
row0 = any(mat[0][j] == 0 for j in range(m))
col0 = any(mat[i][0] == 0 for i in range(n))
for i in range(1, n):
for j in range(1, m):
if mat[i][j] == 0:
mat[i][0] = mat[0][j] = 0
for i in range(1, n):
for j in range(1, m):
if mat[i][0] == 0 or mat[0][j] == 0:
mat[i][j] = 0
if row0:
for j in range(m): mat[0][j] = 0
if col0:
for i in range(n): mat[i][0] = 0
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated