forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of Enclaves.py
37 lines (29 loc) · 981 Bytes
/
Number of Enclaves.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
from typing import List
class Solution:
"""
Time: O(n*m)
Memory: O(n*m)
"""
WATER = 0
LAND = 1
def numEnclaves(self, grid: List[List[int]]) -> int:
n, m = len(grid), len(grid[0])
for i in range(n):
self.sink_island(i, 0, grid)
self.sink_island(i, m - 1, grid)
for j in range(m):
self.sink_island(0, j, grid)
self.sink_island(n - 1, j, grid)
return sum(map(sum, grid))
@classmethod
def sink_island(cls, row: int, col: int, grid: List[List[int]]):
if grid[row][col] == cls.LAND:
grid[row][col] = cls.WATER
if row > 0:
cls.sink_island(row - 1, col, grid)
if row < len(grid) - 1:
cls.sink_island(row + 1, col, grid)
if col < len(grid[0]) - 1:
cls.sink_island(row, col + 1, grid)
if col > 0:
cls.sink_island(row, col - 1, grid)