Skip to content

Commit

Permalink
DaleStudy#283 set-matrix-zeroes solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sungjinwi committed Jan 22, 2025
1 parent e38e191 commit a1b6d1d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions set-matrix-zeroes/sungjinwi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
ํ’€์ด :
dfs๋ฅผ ์ด์šฉํ•ด์„œ ์ธ์ž๋กœ ๋“ค์–ด์˜จ row, col๋ถ€ํ„ฐ matrix๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ 0์„ ์ฐพ๋Š”๋‹ค
0 ์ฐพ์œผ๋ฉด column 1 ์ฆ๊ฐ€์‹œ์ผœ dfsํ˜ธ์ถœ ํ›„ ํ•ด๋‹น ํ–‰,์—ด์„ 0์œผ๋กœ ์„ค์ • ํ›„ return
๋ชจ๋“  matrix ์ˆœํšŒํ•˜๋ฉด return
m * n matrix
TC : O(M * N)
์ „์ฒด matrix๋ฅผ ํ•œ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ
SC : O(M * N)
0์˜ ๊ฐœ์ˆ˜๋งŒํผ ์žฌ๊ท€ํ˜ธ์ถœ์Šคํƒ์ด ์Œ“์ด๋Š”๋ฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ M * N๋งŒํผ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ
- ๋‹ค๋ฅธํ’€์ด
์ฒซ์งธ ํ–‰๊ณผ ์ฒซ์งธ ์—ด์„ ๊ฐ ํ–‰๋ ฌ์— ๋Œ€ํ•œ 0์—ฌ๋ถ€ ์ €์žฅํ•˜๋Š” flag๋กœ ์‚ฌ์šฉ
์ฒซ์งธ ํ–‰๊ณผ ์ฒซ์งธ ์—ด์˜ 0 ์—ฌ๋ถ€๋Š” ๋”ฐ๋กœ ๋ณ€์ˆ˜ 2๊ฐœ๋กœ ์ €์žฅ
๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ O(1)๋กœ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Œ
"""

class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n_rows = len(matrix)
n_cols = len(matrix[0])
def dfs(row: int, col: int) -> None :
while row < n_rows :
while col < n_cols :
if matrix[row][col] == 0 :
dfs(row,col + 1)
for i in range(n_rows) :
matrix[i][col] = 0
for j in range(n_cols) :
matrix[row][j] = 0
return
col += 1
col = 0
row += 1
return
dfs(0,0)

0 comments on commit a1b6d1d

Please sign in to comment.