-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1254. Number of Closed Islands.py
64 lines (49 loc) · 1.95 KB
/
1254. Number of Closed Islands.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import List
# Ignore 0's on the edge
# Run DFS on all other 0's
# Need to run another DFS first to remove islands
# connected to the edge because they mess up soln
class Solution:
result = 0
def closedIsland(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
alreadyVisited = set()
directions = [[1,0],[-1,0],[0,1],[0,-1]]
# We run this DFS to remove islands
# that are on the edge because they
# mess up the soln
def removeIslandsOnEdge(r,c):
alreadyVisited.add((r,c))
grid[r][c] = 1
for dr,dc in directions:
if (r+dr) in range(rows) and (c+dc) in range(cols) and \
(r+dr,c+dc) not in alreadyVisited and grid[r+dr][c+dc] == 0:
removeIslandsOnEdge(r+dr,c+dc)
for i in range(rows):
for j in range(cols):
if grid[i][j] == 0 and \
(i == 0 or j == 0 or
i == rows-1 or j == cols-1) and \
(i,j) not in alreadyVisited:
removeIslandsOnEdge(i,j)
alreadyVisited.clear()
# DFS for finding number of islands
def dfs(r,c):
alreadyVisited.add((r,c))
for dr,dc in directions:
if (r+dr) in range(rows) and \
(c+dc) in range(cols) and \
(r+dr,c+dc) not in alreadyVisited and \
grid[r+dr][c+dc] == 0:
dfs(r+dr,c+dc)
# Run DFS on real islands
for i in range(rows):
for j in range(cols):
if grid[i][j] == 0 and \
(i,j) not in alreadyVisited and \
i != 0 and i != rows-1 and \
j != 0 and j != cols-1:
dfs(i,j)
self.result += 1
return self.result