FastPrepNumber of Islands in Three Dimensions

Number of Islands in Three Dimensions

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

grid[z][r][c] is 1 for land and 0 for water. Land cells belong to the same island when connected through shared faces along depth, row, or column axes. Return the number of islands.

Function

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

Examples

Example 1

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

The two cells at [0,0,0] and [1,0,0] connect by a face; the diagonal cell is separate.

Constraints

  • The rectangular 3D grid contains at most 10^6 cells.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int numIslands3D(int[][][] grid) {
  // Write your code here.
}
grid[[[1,0],[0,1]],[[1,0],[0,0]]]
expected2
Checking account…