forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurrounded-regions.py
More file actions
34 lines (28 loc) · 885 Bytes
/
surrounded-regions.py
File metadata and controls
34 lines (28 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Time: O(N)
Space: O(N)
"""
class Solution:
def solve(self, board: List[List[str]]) -> None:
def dfs(i0, j0):
if i0<0 or j0<0 or i0>=MAX_ROW or j0>=MAX_COL: return
if board[i0][j0]!='O': return
if (i0, j0) in survived: return
survived.add((i0, j0))
dfs(i0+1, j0)
dfs(i0-1, j0)
dfs(i0, j0+1)
dfs(i0, j0-1)
MAX_ROW = len(board)
MAX_COL = len(board[0])
survived = set()
for i in range(MAX_ROW):
dfs(i, 0)
dfs(i, MAX_COL-1)
for j in range(MAX_COL):
dfs(0, j)
dfs(MAX_ROW-1, j)
for i in range(MAX_ROW):
for j in range(MAX_COL):
board[i][j] = 'O' if (i, j) in survived else 'X'
return board