forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of Enclaves.py
30 lines (25 loc) · 1019 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
class Solution:
def recursion(self, grid, row, col, m, n):
if 0<=row<m and 0<=col<n and grid[row][col] == 1:
grid[row][col] = 't'
self.recursion(grid, row+1, col, m, n)
self.recursion(grid, row-1, col, m, n)
self.recursion(grid, row, col+1, m, n)
self.recursion(grid, row, col-1, m, n)
def numEnclaves(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
if not m or not n:
return 0
# mark all boundary lands and neighbors with 0
for row in range(m):
self.recursion(grid, row, 0, m, n)
self.recursion(grid, row, n-1, m, n)
for col in range(n):
self.recursion(grid, 0, col, m, m)
self.recursion(grid, m-1, col, m, n)
result = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
result += 1
return result