-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnumber_of_enclaves.py
42 lines (35 loc) · 1.46 KB
/
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
38
39
40
41
42
# https://leetcode.com/problems/number-of-enclaves/description/
# T: O(m*n)
# S: O(m*n)
from typing import List
class Solution:
def dfs(self, x: int, y: int, m: int, n: int, grid: List[List[int]], visit: List[List[bool]]):
if x < 0 or x >= m or y < 0 or y >= n or visit[x][y] or grid[x][y] == 0:
return
visit[x][y] = True
self.dfs(x+1, y, m, n, grid, visit)
self.dfs(x-1, y, m, n, grid, visit)
self.dfs(x, y+1, m, n, grid, visit)
self.dfs(x, y-1, m, n, grid, visit)
def numEnclaves(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
visit = [[False for _ in range(n)] for _ in range(m)]
# Traverse the first and last columns.
for i in range(m):
if grid[i][0] == 1 and not visit[i][0]:
self.dfs(i, 0, m, n, grid, visit)
if grid[i][n-1] == 1 and not visit[i][n-1]:
self.dfs(i, n-1, m, n, grid, visit)
# Traverse the first and last rows.
for j in range(n):
if grid[0][j] == 1 and not visit[0][j]:
self.dfs(0, j, m, n, grid, visit)
if grid[m-1][j] == 1 and not visit[m-1][j]:
self.dfs(m-1, j, m, n, grid, visit)
# Count the number of unvisited land cells.
count = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 1 and not visit[i][j]:
count += 1
return count