-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
34 lines (30 loc) · 1.05 KB
/
main.py
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
class Solution:
def minDays(self, grid: List[List[int]]) -> int:
if self.numIslands(grid) != 1:
return 0
m, n = len(grid), len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
grid[i][j] = 0
if self.numIslands(grid) != 1:
return 1
grid[i][j] = 1
return 2
def numIslands(self, grid: List[List[int]]) -> int:
directions = ((1, 0), (0, 1), (-1, 0), (0, -1))
vis = set()
m, n = len(grid), len(grid[0])
def dfs(i: int, j: int):
if i < 0 or m <= i or j < 0 or n <= j or grid[i][j] == 0 or (i, j) in vis:
return
vis.add((i, j))
for di, dj in directions:
dfs(i + di, j + dj)
result = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1 and (i, j) not in vis:
dfs(i, j)
result += 1
return result