LeetCode 1254. Number of Closed Islands Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1254. Number of Closed Islands

Description

Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.

Return the number of closed islands.

 

Example 1:

Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
Output: 2
Explanation: 
Islands in gray are closed because they are completely surrounded by water (group of 1s).

Example 2:

Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output: 1

Example 3:

Input: grid = [[1,1,1,1,1,1,1],
               [1,0,0,0,0,0,1],
               [1,0,1,1,1,0,1],
               [1,0,1,0,1,0,1],
               [1,0,1,1,1,0,1],
               [1,0,0,0,0,0,1],
               [1,1,1,1,1,1,1]]
Output: 2

 

Constraints:

  • 1 <= grid.length, grid[0].length <= 100
  • 0 <= grid[i][j] <=1

Solutions

Solution 1: DFS

We traverse the matrix, and for each piece of land, we perform a depth-first search to find all the land connected to it. Then we check if there is any land on the boundary. If there is, it is not a closed island; otherwise, it is a closed island, and we increment the answer by one.

Finally, we return the answer.

The time complexity is O(m × n), and the space complexity is O(m × n). Where m and n are the number of rows and columns in the matrix, respectively.

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

Solution 2: Union-Find

We can use a union-find set to maintain each piece of connected land.

We traverse the matrix, if the current position is on the boundary, we connect it with the virtual node m × n. If the current position is land, we connect it with the land below and to the right.

Then, we traverse the matrix again, for each piece of land, if its root node is itself, we increment the answer by one.

Finally, we return the answer.

The time complexity is O(m × n × Î±(m × n)), and the space complexity is O(m × n). Where m and n are the number of rows and columns in the matrix, respectively.

PythonJavaC++GoTypeScriptC#
class UnionFind: def __init__(self, n: int): self.p = list(range(n)) self.size = [1] * n def find(self, x: int) -> int: if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, a: int, b: int): 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 closedIsland(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) uf = UnionFind(m * n + 1) for i in range(m): for j in range(n): if i == 0 or i == m - 1 or j == 0 or j == n - 1: uf.union(i * n + j, m * n) if grid[i][j] == 0: if i < m - 1 and grid[i + 1][j] == 0: uf.union(i * n + j, (i + 1) * n + j) if j < n - 1 and grid[i][j + 1] == 0: uf.union(i * n + j, i * n + j + 1) ans = 0 for i in range(m): for j in range(n): ans += grid[i][j] == 0 and uf.find(i * n + j) == i * n + j return ans(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 !