File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ from collections import deque
2
+ from typing import List
3
+
4
+ class Solution :
5
+ def numIslands (self , grid : List [List [str ]]) -> int :
6
+ def bfs (x , y ):
7
+ queue = deque ([(x , y )])
8
+ grid [x ][y ] = "0"
9
+
10
+ while queue :
11
+ r , c = queue .popleft ()
12
+
13
+ for d in range (4 ):
14
+ nr , nc = r + dr [d ], c + dc [d ]
15
+ if 0 <= nr < n and 0 <= nc < m and grid [nr ][nc ] == "1" :
16
+ grid [nr ][nc ] = "0"
17
+ queue .append ((nr , nc ))
18
+
19
+ n , m = len (grid ), len (grid [0 ])
20
+
21
+ dr = (- 1 , 1 , 0 , 0 )
22
+ dc = (0 , 0 , - 1 , 1 )
23
+
24
+ result = 0
25
+ for r in range (n ):
26
+ for c in range (m ):
27
+ if grid [r ][c ] == '1' :
28
+ bfs (r , c )
29
+ result += 1
30
+
31
+ return result
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+ class Solution :
3
+ def setZeroes (self , matrix : List [List [int ]]) -> None :
4
+ """
5
+ Do not return anything, modify matrix in-place instead.
6
+ """
7
+ n , m = len (matrix ), len (matrix [0 ])
8
+ rows , cols = set (), set ()
9
+
10
+ for r in range (n ):
11
+ for c in range (m ):
12
+ if matrix [r ][c ] == 0 :
13
+ rows .add (r )
14
+ cols .add (c )
15
+
16
+ for r in range (n ):
17
+ for c in range (m ):
18
+ if r in rows or c in cols :
19
+ matrix [r ][c ] = 0
20
+
Original file line number Diff line number Diff line change
1
+ # 시간 복잡도 O(mn), 공간복잡도 O(mn)
2
+ class Solution :
3
+ def uniquePaths (self , m : int , n : int ) -> int :
4
+ dp = [[1 ] * n for _ in range (m )]
5
+
6
+ for i in range (1 , m ):
7
+ for j in range (1 , n ):
8
+ dp [i ][j ] = dp [i - 1 ][j ] + dp [i ][j - 1 ]
9
+
10
+ return dp [m - 1 ][n - 1 ]
11
+
You can’t perform that action at this time.
0 commit comments