LeetCode - Count Sub Islands (Javascript)
문제 설명
1번 그리드와 2번 그리드를 비교하여, 1번 그리드에 포함되는 2번에서의 섬의 개수를 구하는 문제다.
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:
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]] |
Example 2:
1 | 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]] |
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
.
소스코드
1 | const countSubIslands = (grid1, grid2) => { |
LeetCode - Count Sub Islands (Javascript)
https://hoonjoo-park.github.io/algorithm/leet/countSubIslands/