FastPrepCount Land Islands

Count Land Islands

Roblox logoRoblox● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

For this exercise, assume grid is a rectangular map: 1 is land and 0 is water. An island is a maximal group of land cells connected by shared sides (up, down, left or right). Diagonals do not connect islands.

Return the number of islands. Do not change the input. A grid with no cells has zero islands.

Function

countIslands(grid: int[][]) → int

Examples

Example 1

grid = [[1,1,0,0],[0,1,0,1],[1,0,0,1]]return = 3

The upper-left group, lower-left cell and right-hand group are three islands.

Example 2

grid = [[1,0],[0,1]]return = 2

The two land cells touch only diagonally.

Example 3

grid = []return = 0

The map has no rows.

Constraints

  • 0 <= grid.length <= 60.
  • All rows have the same length c, with 0 <= c <= 60.
  • Every cell is 0 or 1.

More Roblox problems

See Roblox hiring insights
public int countIslands(int[][] grid) {
    // write your code here
}
grid[[1,1,0,0],[0,1,0,1],[1,0,0,1]]
expected3
Checking account…