Skip to content

Commit 7cbc714

Browse files
committed
feat: [Week 07-5] solve Set Matrix Zeroes
1 parent bde92ae commit 7cbc714

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Solution:
3+
1) matrix๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ 0์„ ์ฐพ๋Š”๋‹ค.
4+
2) 0์ผ ๊ฒฝ์šฐ rows, cols set์— ๊ฐ ์ธ๋ฑ์Šค๋ฅผ ๋„ฃ๋Š”๋‹ค.
5+
3) rows, cols ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ํ•ด๋‹นํ•˜๋Š” row, col์„ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
6+
Time: O(nm) = O(nm) (์ˆœํšŒ) + ์ตœ๋Œ€ O(nm) + ์ตœ๋Œ€ O(nm)
7+
Space: O(n + m)
8+
"""
9+
10+
11+
class Solution:
12+
def setZeroes(self, matrix: List[List[int]]) -> None:
13+
"""
14+
Do not return anything, modify matrix in-place instead.
15+
"""
16+
ROWS, COLS = len(matrix), len(matrix[0])
17+
rows = set()
18+
cols = set()
19+
for i in range(ROWS):
20+
for j in range(COLS):
21+
if matrix[i][j] == 0:
22+
rows.add(i)
23+
cols.add(j)
24+
25+
for i in rows:
26+
for j in range(COLS):
27+
matrix[i][j] = 0
28+
29+
for j in cols:
30+
for i in range(ROWS):
31+
matrix[i][j] = 0

0 commit comments

Comments
ย (0)