-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMatrixZeroes_Day14.py
More file actions
71 lines (60 loc) · 2.05 KB
/
Copy pathSetMatrixZeroes_Day14.py
File metadata and controls
71 lines (60 loc) · 2.05 KB
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
69
70
71
#Brute Approach
# class Solution:
# def setZeroes(self, matrix: List[List[int]]) -> None:
# rows, cols = len(matrix), len(matrix[0])
# temp = [[1] * cols for _ in range(rows)]
#
# for i in range(rows):
# for j in range(cols):
# if matrix[i][j] == 0:
# for k in range(cols):
# temp[i][k] = 0
# for k in range(rows):
# temp[k][j] = 0
#
# for i in range(rows):
# for j in range(cols):
# if temp[i][j] == 0:
# matrix[i][j] = 0
# Time Complexity: O(m * n * (m + n))
# Space Complexity: O(1) if using -1 trick, otherwise O(m + n)
#Better Approach
# class Solution:
# def setZeroes(self, matrix: List[List[int]]) -> None:
# rows, cols = len(matrix), len(matrix[0])
# row_flags = [0] * rows
# col_flags = [0] * cols
#
# for i in range(rows):
# for j in range(cols):
# if matrix[i][j] == 0:
# row_flags[i] = 1
# col_flags[j] = 1
#
# for i in range(rows):
# for j in range(cols):
# if row_flags[i] == 1 or col_flags[j] == 1:
# matrix[i][j] = 0
# Time Complexity: O(m * n)
# Space Complexity: O(m + n)
#Optimal Approach
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
rows, cols = len(matrix), len(matrix[0])
col0 = 1
for i in range(rows):
if matrix[i][0] == 0:
col0 = 0
for j in range(1, cols):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[i][j] = 0
matrix[0][j] = 0
for i in reversed(range(rows)):
for j in reversed(range(1, cols)):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if col0 == 0:
matrix[i][0] = 0
# Time Complexity: O(m * n)
# Space Complexity: O(1) (in-place)