File tree Expand file tree Collapse file tree 1 file changed +8
-5
lines changed
Python/chapter01/1.8 - Zero Matrix Expand file tree Collapse file tree 1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change 1
1
"""
2
2
Python version 3.7.0
3
3
1.8 - Zero Matrix
4
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0
4
+ Write an algorithm such that if an element in an MxN matrix is 0,
5
+ its entire row and column are set to 0
5
6
"""
6
7
import unittest
7
8
from typing import List
8
9
9
10
10
11
def zero_matrix (matrix : List [List [int ]]) -> List [List [int ]]:
11
12
"""
12
- zero_matrix will take in an MxN matrix and when a 0 element is found, the whole column and row
13
- will be set to 0. The algorithm works by first scanning for rows and columns where there is a 0
14
- and store into a set.
15
- Then we go through each stored row and column indices and proceed to set the rows and columns to 0.
13
+ zero_matrix will take in an MxN matrix and when a 0 element is found,
14
+ the whole column and row will be set to 0.
15
+ The algorithm works by first scanning for
16
+ rows and columns where there is a 0 and store into a set.
17
+ Then we go through each stored row and column indices
18
+ and proceed to set the rows and columns to 0.
16
19
Runtime: O(M * N)
17
20
Space Complexity: O(M + N)
18
21
:param matrix: an MxN matrix. M is the number of rows, N is the number of columns
You can’t perform that action at this time.
0 commit comments