-
Notifications
You must be signed in to change notification settings - Fork 43
/
set-matrix-zeroes.py
321 lines (276 loc) · 9.93 KB
/
set-matrix-zeroes.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
73. Set Matrix Zeroes
Medium
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
You must do it in place.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:
Input: matrix = [[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]]
Constraints:
m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
-231 <= matrix[i][j] <= 231 - 1
Follow up:
A straightforward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
"""
# V0
# IDEA : array op
class Solution(object):
def setZeroes(self, matrix):
# edge case
if not matrix:
return
l = len(matrix)
w = len(matrix[0])
# get zeros
x_zeros = set()
y_zeros = set()
for i in range(l):
for j in range(w):
if matrix[i][j] == 0:
x_zeros.add(j)
y_zeros.add(i)
# row -> 0
for i in y_zeros:
matrix[i] = [0] * w
# col -> 0
for i in x_zeros:
for j in range(l):
matrix[j][i] = 0
#print ("matrix = " + str(matrix))
# V0'
# IDEA : array op
class Solution(object):
def setZeroes(self, matrix):
if not matrix:
return matrix
def help(matrix, xy):
### NOTE :
# -> for cases matrix[i][j]:
# -> y is FIRST element (i)
# -> x is SECOND element (j)
x = xy[1]
y = xy[0]
matrix[y] = [0] * len(matrix[0])
for j in range(len(matrix)):
matrix[j][x] = 0
return matrix
_list = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 0:
_list.append([i,j])
for xy in _list:
matrix = help(matrix, xy)
return matrix
# V0''
# IDEA : array op
class Solution:
def setZeroes(self, matrix):
rownum = len(matrix)
colnum = len(matrix[0])
row = [False for i in range(rownum)]
col = [False for i in range(colnum)]
for i in range(rownum):
for j in range(colnum):
if matrix[i][j] == 0:
row[i] = True
col[j] = True
for i in range(rownum):
for j in range(colnum):
if row[i] == True or col[j] == True:
matrix[i][j] = 0
# V1
# https://www.cnblogs.com/zuoyuan/p/3769698.html
# TIME COMPLEXITY : O(N*M)
# SPACE COMPLEXITY : O(N+M)
# IDEA : RECORD X, Y (X, Y AXIS) TO CHECK IF THERE IS 0 EXISTING
# THEN GO THROUGH ARRAY TO CHECK RELATIVE ROWS AND COLUMNS
# AND UPDATE THE VALUES (value -> 0)
class Solution:
# @param matrix, a list of lists of integers
# RETURN NOTHING, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
rownum = len(matrix)
colnum = len(matrix[0])
row = [False for i in range(rownum)]
col = [False for i in range(colnum)]
for i in range(rownum):
for j in range(colnum):
if matrix[i][j] == 0:
row[i] = True
col[j] = True
for i in range(rownum):
for j in range(colnum):
if row[i] or col[j]:
matrix[i][j] = 0
# for test case
#return matrix
### Test case
s=Solution()
assert s.setZeroes([[]]) == [[]]
assert s.setZeroes([[0]]) == [[0]]
assert s.setZeroes([[1]]) == [[1]]
assert s.setZeroes([[1,2,3]]) == [[1,2,3]]
assert s.setZeroes([[0,2,3]]) == [[0,0,0]]
assert s.setZeroes([[1,2,3],[4,5,6]]) == [[1,2,3],[4,5,6]]
assert s.setZeroes([[1,2,3], [4,5,6], [7,8,9]]) == [[1,2,3], [4,5,6], [7,8,9]]
assert s.setZeroes([[1,2,3], [4,0,6], [7,8,9]]) == [[1,0,3], [0,0,0], [7,0,9]]
assert s.setZeroes([[1,2,3,999], [4,0,6,0], [0,7,8,9]]) == [[0, 0, 3, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
assert s.setZeroes([[0,0,0], [0,0,0], [0,0,0]]) == [[0,0,0], [0,0,0], [0,0,0]]
# V1''
# https://leetcode.com/problems/set-matrix-zeroes/solution/
# IDEA : BRUTE FORCE + DOUBLE LOOP
# TIME COMPLEXITY : O(N*M)
# SPACE COMPLEXITY : O(N+M)
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
R = len(matrix)
C = len(matrix[0])
rows, cols = set(), set()
# Essentially, we mark the rows and columns that are to be made zero
for i in range(R):
for j in range(C):
if matrix[i][j] == 0:
rows.add(i)
cols.add(j)
# Iterate over the array once again and using the rows and cols sets, update the elements
for i in range(R):
for j in range(C):
if i in rows or j in cols:
matrix[i][j] = 0
# V1'''
# https://leetcode.com/problems/set-matrix-zeroes/solution/
# TIME COMPLEXITY : O((N*M))
# SPACE COMPLEXITY : O(1)
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
is_col = False
R = len(matrix)
C = len(matrix[0])
for i in range(R):
# Since first cell for both first row and first column is the same i.e. matrix[0][0]
# We can use an additional variable for either the first row/column.
# For this solution we are using an additional variable for the first column
# and using matrix[0][0] for the first row.
if matrix[i][0] == 0:
is_col = True
for j in range(1, C):
# If an element is zero, we set the first element of the corresponding row and column to 0
if matrix[i][j] == 0:
matrix[0][j] = 0
matrix[i][0] = 0
# Iterate over the array once again and using the first row and first column, update the elements.
for i in range(1, R):
for j in range(1, C):
if not matrix[i][0] or not matrix[0][j]:
matrix[i][j] = 0
# See if the first row needs to be set to zero as well
if matrix[0][0] == 0:
for j in range(C):
matrix[0][j] = 0
# See if the first column needs to be set to zero as well
if is_col:
for i in range(R):
matrix[i][0] = 0
# V1'''
# https://leetcode.com/problems/set-matrix-zeroes/solution/
# TIME COMPLEXITY : O((N*M)*(N+M))
# SPACE COMPLEXITY : O(1)
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
MODIFIED = -1000000
R = len(matrix)
C = len(matrix[0])
for r in range(R):
for c in range(C):
if matrix[r][c] == 0:
# We modify the elements in place. Note, we only change the non zeros to MODIFIED
for k in range(C):
matrix[r][k] = MODIFIED if matrix[r][k] != 0 else 0
for k in range(R):
matrix[k][c] = MODIFIED if matrix[k][c] != 0 else 0
for r in range(R):
for c in range(C):
# Make a second pass and change all MODIFIED elements to 0 """
if matrix[r][c] == MODIFIED:
matrix[r][c] = 0
# V1''''
# https://blog.csdn.net/qqxx6661/article/details/78279728
class Solution:
# @param matrix, a list of lists of integers
# RETURN NOTHING, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
m , n = len(matrix), len(matrix[0])
row , col = [0 for i in range(m)] , [0 for i in range(n)]
for i in range(m):
for j in range(n):
if not matrix[i][j]:
row[i]=col[j]=1
for i in range(m):
if row[i]:
for j in range(n):
matrix[i][j]=0
for j in range(n):
if col[j]:
for i in range(m):
matrix[i][j]=0
# V1'''''''
# https://blog.csdn.net/qqxx6661/article/details/78279728
class Solution:
# @param matrix, a list of lists of integers
# RETURN NOTHING, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
m , n = len(matrix), len(matrix[0])
temp = [[matrix[i][j] for j in range(n)] for i in range(m)]
for i in range(m):
for j in range(n):
if not temp[i][j]:
self.setZero(i,j,n,m,matrix)
def setZero(self,row,col,n,m,matrix):
for i in range(m):
matrix[i][col]=0
for j in range(n):
matrix[row][j]=0
# V2
from functools import reduce
# Time: O(m * n)
# Space: O(1)
class Solution(object):
# @param matrix, a list of lists of integers
# RETURN NOTHING, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
first_col = reduce(lambda acc, i: acc or matrix[i][0] == 0, range(len(matrix)), False)
first_row = reduce(lambda acc, j: acc or matrix[0][j] == 0, range(len(matrix[0])), False)
for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if matrix[i][j] == 0:
matrix[i][0], matrix[0][j] = 0, 0
for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0
if first_col:
for i in range(len(matrix)):
matrix[i][0] = 0
if first_row:
for j in range(len(matrix[0])):
matrix[0][j] = 0