Skip to content

Commit 47d881a

Browse files
committed
feat(arrays, matrices): set matrix to zero
1 parent 9f5abb3 commit 47d881a

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Set Matrix Zero
2+
3+
Given a matrix, mat, if any element within the matrix is zero, set that row and column to zero. The performed operations
4+
should be in place, i.e., the given matrix is modified directly without allocating another matrix.
5+
6+
Constraints:
7+
8+
Let `mat` be the matrix given as input to find the anagrams.
9+
10+
- 1 <= `mat.row`, `mat.col` <= 20
11+
- -2^31 <= `mat[i][j]` <= 2^31-1
12+
13+
## Examples
14+
15+
![Example one](images/set_matrix_zero_example_one.png)
16+
![Example two](images/set_matrix_zero_example_two.png)
17+
![Example three](images/set_matrix_zero_example_three.png)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import List
2+
3+
4+
def set_matrix_zeros(mat: List[List[int]]) -> List[List[int]]:
5+
"""
6+
Sets all elements in a matrix to zero if any row or column has at least one zero element.
7+
8+
## Complexity Analysis
9+
10+
This solution is efficient in both time and space. The time complexity is O(rows × cols) because we scan through
11+
the matrix a constant number of times. The space complexity is O(1) because we only use a couple of boolean
12+
variables regardless of the matrix size, and we use the matrix itself for tracking.
13+
14+
Args:
15+
mat (List): input matrix
16+
Returns:
17+
List[List[int]]modified matrix with all elements in any row or column containing a zero element set to zero
18+
"""
19+
if not mat or not mat[0]:
20+
return mat
21+
22+
row_length, col_length = len(mat), len(mat[0])
23+
24+
# Step 1: Check if the first row and first column originally contain zeros
25+
# We need this info because we'll use them as markers
26+
first_row_has_zero = any(mat[0][j] == 0 for j in range(col_length))
27+
first_col_has_zero = any(mat[i][0] == 0 for i in range(row_length))
28+
29+
# Step 2: Use first row and column as markers for other rows/columns
30+
# Scan the interior of the matrix (excluding first row and column)
31+
for r in range(1, row_length):
32+
for c in range(1, col_length):
33+
if mat[r][c] == 0:
34+
# Mark this row and column by setting the corresponding
35+
# positions in the first row and column to zero
36+
mat[r][0] = 0 # mark row as 0
37+
mat[0][c] = 0 # mark column as 0
38+
39+
# Step 3: Use the markers to set zeros in the interior of the matrix
40+
# We process from [1][1] onwards to avoid corrupting our markers
41+
for r in range(1, row_length):
42+
for c in range(1, col_length):
43+
# If this row or column was marked, set this cell to zero
44+
if mat[r][0] == 0 or mat[0][c] == 0:
45+
mat[r][c] = 0
46+
47+
# Step 4: Finally, handle the first row and column based on our original checks
48+
if first_row_has_zero:
49+
for c in range(col_length):
50+
mat[0][c] = 0
51+
if first_col_has_zero:
52+
for r in range(row_length):
53+
mat[r][0] = 0
54+
55+
return mat
60.3 KB
Loading
81.5 KB
Loading
66 KB
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
from . import set_matrix_zeros
3+
4+
5+
class SetMatrixZeroTestCase(unittest.TestCase):
6+
def test_1(self):
7+
matrix = [[1,2,3],[4,5,6],[7,0,9]]
8+
expected = [[1,0,3],[4,0,6],[0,0,0]]
9+
actual = set_matrix_zeros(matrix)
10+
self.assertEqual(expected, actual)
11+
12+
def test_2(self):
13+
matrix = [[1,2,3,4],[4,5,6,7],[8,9,4,6]]
14+
expected = [[1,2,3,4],[4,5,6,7],[8,9,4,6]]
15+
actual = set_matrix_zeros(matrix)
16+
self.assertEqual(expected, actual)
17+
18+
def test_3(self):
19+
matrix = [[1,1,0,1,1],[1,1,1,1,1],[1,1,1,1,1],[1,0,1,1,1],[1,1,1,1,1]]
20+
expected = [[0, 0, 0, 0, 0], [1, 0, 0, 1, 1], [1, 0, 0, 1, 1], [0, 0, 0, 0, 0], [1, 0, 0, 1, 1]]
21+
actual = set_matrix_zeros(matrix)
22+
self.assertEqual(expected, actual)
23+
24+
def test_4(self):
25+
matrix = [[1,1,1],[1,0,1],[1,1,1]]
26+
expected = [[1, 0, 1], [0, 0, 0], [1, 0, 1]]
27+
actual = set_matrix_zeros(matrix)
28+
self.assertEqual(expected, actual)
29+
30+
def test_5(self):
31+
matrix = [[3,5,2,0],[1,0,4,6],[7,3,2,4]]
32+
expected = [[0, 0, 0, 0], [0, 0, 0, 0], [7, 0, 2, 0]]
33+
actual = set_matrix_zeros(matrix)
34+
self.assertEqual(expected, actual)
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

0 commit comments

Comments
 (0)