Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1905.html
1905. Count Sub Islands
Level
Medium
Description
You are given two m x n
binary matrices grid1
and grid2
containing only 0
’s (representing water) and 1
’s (representing land). An island is a group of 1
’s connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells.
An island in grid2
is considered a sub-island if there is an island in grid1
that contains all the cells that make up this island in grid2
.
Return the number** of islands in grid2
that are considered **sub-islands.
Example 1:
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
Example 2:
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
Output: 2
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
Constraints:
m == grid1.length == grid2.length
n == grid1[i].length == grid2[i].length
1 <= m, n <= 500
grid1[i][j]
andgrid2[i][j]
are either0
or1
.
Solution
Do breadth first search on the islands in grid2
. For each island in grid2
, it is a sub-island if and only if all the cells in the island are lands in grid1
. After each island in grid2
is visited, it can be determined whether the island is a sub-island. The number of sub-islands can be calculated as well.
-
class Solution { static int[][] directions = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; public int countSubIslands(int[][] grid1, int[][] grid2) { int subIslands = 0; int m = grid1.length, n = grid1[0].length; boolean[][] visited = new boolean[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid2[i][j] == 1 && !visited[i][j]) { boolean isSubisland = breadthFirstSearch(grid1, grid2, visited, m, n, i, j); if (isSubisland) subIslands++; } } } return subIslands; } public boolean breadthFirstSearch(int[][] grid1, int[][] grid2, boolean[][] visited, int m, int n, int startRow, int startColumn) { boolean isSubisland = true; Queue<int[]> queue = new LinkedList<int[]>(); queue.offer(new int[]{startRow, startColumn}); visited[startRow][startColumn] = true; while (!queue.isEmpty()) { int[] cell = queue.poll(); int row = cell[0], column = cell[1]; if (grid1[row][column] == 0) isSubisland = false; for (int[] direction : directions) { int newRow = row + direction[0], newColumn = column + direction[1]; if (newRow >= 0 && newRow < m && newColumn >= 0 && newColumn < n && grid2[newRow][newColumn] == 1 && !visited[newRow][newColumn]) { visited[newRow][newColumn] = true; queue.offer(new int[]{newRow, newColumn}); } } } return isSubisland; } } ############ class Solution { public int countSubIslands(int[][] grid1, int[][] grid2) { int m = grid1.length; int n = grid1[0].length; int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid2[i][j] == 1 && dfs(i, j, m, n, grid1, grid2)) { ++ans; } } } return ans; } private boolean dfs(int i, int j, int m, int n, int[][] grid1, int[][] grid2) { boolean ans = grid1[i][j] == 1; grid2[i][j] = 0; int[] dirs = {-1, 0, 1, 0, -1}; for (int k = 0; k < 4; ++k) { int x = i + dirs[k]; int y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] == 1 && !dfs(x, y, m, n, grid1, grid2)) { ans = false; } } return ans; } }
-
class Solution: def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int: def dfs(i, j): ans = grid1[i][j] == 1 grid2[i][j] = 0 for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]: x, y = i + a, j + b if 0 <= x < m and 0 <= y < n and grid2[x][y] == 1 and not dfs(x, y): ans = False return ans m, n = len(grid1), len(grid1[0]) return sum(grid2[i][j] == 1 and dfs(i, j) for i in range(m) for j in range(n)) ############ # 1905. Count Sub Islands # https://leetcode.com/problems/count-sub-islands/ class Solution: def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int: rows, cols = len(grid1), len(grid1[0]) res = 0 def dfs(x, y, path): grid2[x][y] = 0 path.append((x, y)) for dx, dy in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)): if 0 <= dx < rows and 0 <= dy < cols and grid2[dx][dy] == 1: dfs(dx, dy, path) for i in range(rows): for j in range(cols): if grid1[i][j] == grid2[i][j] == 1: path = [] dfs(i, j, path) if all(grid1[x][y] == 1 for x, y in path): res += 1 return res
-
class Solution { public: int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) { int m = grid1.size(); int n = grid1[0].size(); int ans = 0; for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) if (grid2[i][j] == 1 && dfs(i, j, m, n, grid1, grid2)) ++ans; return ans; } bool dfs(int i, int j, int m, int n, vector<vector<int>>& grid1, vector<vector<int>>& grid2) { bool ans = grid1[i][j]; grid2[i][j] = 0; vector<int> dirs = {-1, 0, 1, 0, -1}; for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] && !dfs(x, y, m, n, grid1, grid2)) ans = false; } return ans; } };
-
func countSubIslands(grid1 [][]int, grid2 [][]int) int { m, n := len(grid1), len(grid1[0]) ans := 0 var dfs func(i, j int) bool dfs = func(i, j int) bool { res := grid1[i][j] == 1 grid2[i][j] = 0 dirs := []int{-1, 0, 1, 0, -1} for k := 0; k < 4; k++ { x, y := i+dirs[k], j+dirs[k+1] if x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] == 1 && !dfs(x, y) { res = false } } return res } for i := 0; i < m; i++ { for j := 0; j < n; j++ { if grid2[i][j] == 1 && dfs(i, j) { ans++ } } } return ans }
-
function countSubIslands(grid1: number[][], grid2: number[][]): number { let m = grid1.length, n = grid1[0].length; let ans = 0; for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (grid2[i][j] == 1 && dfs(grid1, grid2, i, j)) { ++ans; } } } return ans; } function dfs( grid1: number[][], grid2: number[][], i: number, j: number, ): boolean { let m = grid1.length, n = grid1[0].length; let ans = true; if (grid1[i][j] == 0) { ans = false; } grid2[i][j] = 0; for (let [dx, dy] of [ [0, 1], [0, -1], [1, 0], [-1, 0], ]) { let x = i + dx, y = j + dy; if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || grid2[x][y] == 0) { continue; } if (!dfs(grid1, grid2, x, y)) { ans = false; } } return ans; }