Given a two-dimensional binary grid grid
displaying a map where 1
s signify land and 0
s signify water, determine the number of distinct islands.
An island is defined as a group of 1
s connected horizontally or vertically, and is surrounded by 0
s or the edge of the grid. It is assumed that the grid is completely surrounded by water.
grid: number[][]
: A 2D array of where each element is 1
or 0
Input: grid = [[1,0],[0,0],[0,1],[0,1],[1,1]]Output: 2Explanation: There are 2 islands formed by connecting adjacent lands
Input: grid = [[1,0,0],[1,1,1],[0,0,1]]Output: 1Explanation: There is one island formed by connecting adjacent lands
Input: grid = [[1,1,1],[0,0,0],[0,0,0]]Output: 1Explanation: There is one island formed by connecting adjacent lands
grid.length
, grid[i].length
<= 100grid[i][j]
is 0
or 1
Given a two-dimensional binary grid grid
displaying a map where 1
s signify land and 0
s signify water, determine the number of distinct islands.
An island is defined as a group of 1
s connected horizontally or vertically, and is surrounded by 0
s or the edge of the grid. It is assumed that the grid is completely surrounded by water.
grid: number[][]
: A 2D array of where each element is 1
or 0
Input: grid = [[1,0],[0,0],[0,1],[0,1],[1,1]]Output: 2Explanation: There are 2 islands formed by connecting adjacent lands
Input: grid = [[1,0,0],[1,1,1],[0,0,1]]Output: 1Explanation: There is one island formed by connecting adjacent lands
Input: grid = [[1,1,1],[0,0,0],[0,0,0]]Output: 1Explanation: There is one island formed by connecting adjacent lands
grid.length
, grid[i].length
<= 100grid[i][j]
is 0
or 1
console.log()
statements will appear here.