File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ #ํด์
2
+ # matrix์ ๋ชจ๋ element๋ฅผ ๊ฒ์ฌํ๋ค, ๋ง์ฝ 0์ ๋ฐ๊ฒฌํ๋ฉด ํด๋น element์ ๊ฐ์ row์ col์ ๊ฐ์ง element๋ฅผ "a" ๋ก ๋ฐ๊พผ๋ค.
3
+ # ๋๋ฒ์งธ๋ก matrix์ ๋ชจ๋ element๋ฅผ ๊ฒ์ฌํ๋ค, ๋ง์ฝ a๋ฅผ ๋ฐ๊ฒฌํ๋ฉด ์ด๋ฅผ 0์ผ๋ก ๋ฐ๊พผ๋ค.
4
+
5
+
6
+ #Big O
7
+ #- N: row์ ํฌ๊ธฐ(matrix ํ์ ๊ฐฏ์)
8
+ #- K: col์ ํฌ๊ธฐ(matrix ์ด์ ๊ฐฏ์ )
9
+
10
+ #Time Complexity: O(N*K*(N+K))
11
+ #- for nested loop : ํ์ ๊ฐฏ์(N) ๋น ์ด์ ๊ฐฏ์๋งํผ(K) ๋ฃจํ ์๋ -> O(N*K)
12
+ # - ์ต์
์ ๊ฒฝ์ฐ, ์ฒซ๋ฒ์งธ ๋ฃจํ์์ for i in range(rows)๊ฐ M๋ฒ ๋ฐ๋, for j in range(cols)๊ฐ N๋ฒ ๋ฐ๋ -> O(N+K)
13
+
14
+ #Space Complexity: O(1)
15
+ #- rows, cols: ๋ณ์์ ํ ๋น๊ณผ ์
๋ฐ์ดํธ๋ ์์ ์ทจ๊ธํ๋ค -> O(1)
16
+ from typing import List
17
+
18
+
19
+ class Solution :
20
+ def setZeroes (self , matrix : List [List [int ]]) -> None :
21
+ """
22
+ Do not return anything, modify matrix in-place instead.
23
+ """
24
+ rows , cols = len (matrix ), len (matrix [0 ]) #rows์ cols์ matrix์ ์ขํ ๋ถ์ฌ
25
+
26
+ # 1์ฐจ matrix ์ํ: 0์ ๋ฐ๊ฒฌํ๋ฉด, ๊ทธ element์ ๊ฐ์ ํ๊ณผ ์ด์ 0์ด ์๋ ์๋ฅผ ์์ ๊ฐ "a"๋ก ๋ฐ๊พธ๊ธฐ
27
+ for r in range (rows ):
28
+ for c in range (cols ):
29
+ if matrix [r ][c ] == 0 :
30
+ # ํด๋น ํ๊ณผ ์ด์ 0์ด ์๋ ๋ชจ๋ ๊ฐ์ ์์๋ก "a"๋ก ๋ณ๊ฒฝ
31
+ for i in range (rows ): # ํด๋น ์ด์ ๋ชจ๋ ๊ฐ
32
+ if matrix [i ][c ] != 0 :
33
+ matrix [i ][c ] = "a"
34
+ for j in range (cols ): # ํด๋น ํ์ 0์ด ์๋ ๋ชจ๋ ๊ฐ์ "a"๋ก ๋ณ๊ฒฝ
35
+ if matrix [r ][j ] != 0 :
36
+ matrix [r ][j ] = "a"
37
+
38
+ # 2์ฐจ matrix์ํ: "a"๋ฅผ ๊ฐ์ง ์๋ฅผ 0์ผ๋ก ๋ฐ๊ฟ์ค๋ค.
39
+ for r in range (rows ):
40
+ for c in range (cols ):
41
+ if matrix [r ][c ] == "a" :
42
+ matrix [r ][c ] = 0
43
+
44
+
45
+
You canโt perform that action at this time.
0 commit comments