A HACKERBREWERY ORIGINAL
Islands in the brew
MediumGraphsDepth-first search
A rectangular grid contains 1 for land and 0 for water. Count the islands. Land cells belong to the same island when they touch horizontally or vertically; touching at a corner does not connect them. Return zero for an empty grid.
Example 1
INPUT
[[1,1,0],[0,0,1],[1,0,1]]OUTPUT3The upper-left pair, lower-left cell, and right-hand pair form three islands.
Example 2
INPUT
[[1,0],[0,1]]OUTPUT2Diagonal neighbors are separate islands.
Keep in mind
- 0 ≤ rows, columns ≤ 100
- Every row has the same length.
- Cells contain only 0 or 1.
Draft saved on this browserSpaces: 2 UTF-8
INPUT
[[1,1,0],[0,0,1],[1,0,1]]EXPECTED OUTPUT
3Public samples only. These checks do not award ranked points.