Skip to content

Commit df049c4

Browse files
committed
set-matrix-zeroes sol (py)
1 parent cda9b06 commit df049c4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
https://leetcode.com/problems/set-matrix-zeroes/description/
3+
4+
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
5+
You must do it in place.
6+
7+
TC: O(m * n)
8+
SC: O(m + n), set ์ž๋ฃŒ๊ตฌ์กฐ ์‚ฌ์šฉ
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 = len(matrix)
17+
cols = len(matrix[0])
18+
19+
zero_rows = set() # ์ตœ๋Œ€ m๊ฐœ์˜ ํ–‰ ์ธ๋ฑ์Šค ์ €์žฅ
20+
zero_cols = set() # ์ตœ๋Œ€ n๊ฐœ์˜ ์—ด ์ธ๋ฑ์Šค ์ €์žฅ
21+
22+
# 0์ด ์žˆ๋Š” ์œ„์น˜ ์ฐพ๊ธฐ
23+
for i in range(rows):
24+
for j in range(cols):
25+
if matrix[i][j] == 0:
26+
zero_rows.add(i)
27+
zero_cols.add(j)
28+
29+
# 0์ด ์žˆ๋Š” ํ–‰๊ณผ ์—ด ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ
30+
for i in range(rows):
31+
for j in range(cols):
32+
if i in zero_rows or j in zero_cols:
33+
matrix[i][j] = 0

0 commit comments

Comments
ย (0)