LeetCode 1020. Number of Enclaves Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1020. Number of Enclaves

Description

You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.

Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

 

Example 1:

Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.

Example 2:

Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: All 1s are either on the boundary or can reach the boundary.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 500
  • grid[i][j] is either 0 or 1.

Solutions

Solution 1

PythonJavaC++GoTypeScriptRust
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: def dfs(i: int, j: int): grid[i][j] = 0 for a, b in pairwise(dirs): x, y = i + a, j + b if 0 <= x < m and 0 <= y < n and grid[x][y]: dfs(x, y) m, n = len(grid), len(grid[0]) dirs = (-1, 0, 1, 0, -1) for j in range(n): if grid[0][j]: dfs(0, j) if grid[m - 1][j]: dfs(m - 1, j) for i in range(m): if grid[i][0]: dfs(i, 0) if grid[i][n - 1]: dfs(i, n - 1) return sum(sum(row) for row in grid)(code-box)

Solution 2

PythonJavaC++GoTypeScript
class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) q = deque() for i in range(m): for j in range(n): if grid[i][j] and (i == 0 or i == m - 1 or j == 0 or j == n - 1): q.append((i, j)) grid[i][j] = 0 dirs = (-1, 0, 1, 0, -1) while q: i, j = q.popleft() for a, b in pairwise(dirs): x, y = i + a, j + b if x >= 0 and x < m and y >= 0 and y < n and grid[x][y]: q.append((x, y)) grid[x][y] = 0 return sum(v for row in grid for v in row)(code-box)

Solution 3

PythonJavaC++Go
class UnionFind: def __init__(self, n): self.p = list(range(n)) self.size = [1] * n def find(self, x): if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, a, b): pa, pb = self.find(a), self.find(b) if pa != pb: if self.size[pa] > self.size[pb]: self.p[pb] = pa self.size[pa] += self.size[pb] else: self.p[pa] = pb self.size[pb] += self.size[pa] class Solution: def numEnclaves(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) uf = UnionFind(m * n + 1) dirs = (-1, 0, 1, 0, -1) for i, row in enumerate(grid): for j, v in enumerate(row): if v: if i == 0 or i == m - 1 or j == 0 or j == n - 1: uf.union(i * n + j, m * n) else: for a, b in pairwise(dirs): x, y = i + a, j + b if x >= 0 and x < m and y >= 0 and y < n and grid[x][y]: uf.union(i * n + j, x * n + y) return sum( grid[i][j] == 1 and uf.find(i * n + j) != uf.find(m * n) for i in range(m) for j in range(n) )(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !