-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp1_8.py
68 lines (50 loc) · 1.37 KB
/
p1_8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import List
# Method: Individual cell checks, with handling of first line and col
# Time: O(n*m)
# Space: O(1)
def zero_m(matric: List[List[int]]):
rows, cols = len(matric), len(matric[0])
fcol0 = False
frow0 = False
for r in range(rows):
if matric[r][0] == 0:
fcol0 = True
break
for c in range(cols):
if matric[0][c] == 0:
frow0 = True
break
for r in range(rows):
for c in range(cols):
if matric[r][c] == 0:
matric[r][0] = 0
matric[0][c] = 0
print(f"Gonna zero now, first row {frow0} and first col {fcol0}")
mrint(matric)
for r in range(1, rows):
if matric[r][0] == 0:
for c in range(cols):
matric[r][c] = 0
for c in range(1, cols):
if matric[0][c] == 0:
for r in range(rows):
matric[r][c] = 0
if fcol0:
for r in range(rows):
matric[r][0] = 0
if frow0:
for c in range(cols):
matric[0][c] = 0
return matric
def mrint(matrix: List[List[int]]):
for line in matrix:
print(line)
if __name__ == "__main__":
ex1 = [[1, 2, 3],
[8, 0, 4],
[7, 6, 5]]
mrint(zero_m(ex1))
print("-"*50)
ex2 = [[0, 1, 2],
[4, 5, 6]]
mrint(zero_m(ex2))