Class Solution
- java.lang.Object
-
- g1901_2000.s1905_count_sub_islands.Solution
-
public class Solution extends Object
1905 - Count Sub Islands.Medium
You are given two
m x n
binary matricesgrid1
andgrid2
containing only0
’s (representing water) and1
’s (representing land). An island is a group of1
’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 ingrid1
that contains all the cells that make up this island ingrid2
.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
.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
countSubIslands(int[][] grid1, int[][] grid2)
-