forked from black-shadows/LeetCode-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber-of-closed-islands.py
32 lines (30 loc) · 960 Bytes
/
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
# Time: O(m * n)
# Space: O(1)
class Solution(object):
def closedIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def fill(grid, i, j):
if not (0 <= i < len(grid) and
0 <= j < len(grid[0]) and
grid[i][j] == 0):
return False
grid[i][j] = 1
for dx, dy in directions:
fill(grid, i+dx, j+dy)
return True
for j in xrange(len(grid[0])):
fill(grid, 0, j)
fill(grid, len(grid)-1, j)
for i in xrange(1, len(grid)):
fill(grid, i, 0)
fill(grid, i, len(grid[0])-1)
result = 0
for i in xrange(1, len(grid)-1):
for j in xrange(1, len(grid[0])-1):
if fill(grid, i, j):
result += 1
return result