The Archipelago Count

HardAcc. 82.1%
+50 XP 25

Connectivity in Grids

An island is surrounded by water (0s) and is formed by connecting adjacent 1s horizontally or vertically.

The Assignment

Your function receives a 2D grid of strings "1" (land) and "0" (water).

  1. Scan every cell.
  2. If you find a "1", count a new island and spark a DFS to turn all connected "1"s into "0"s.
  3. Print the final count.

01EXAMPLE 1

Input[["1","1","0"],["0","0","1"]]
Output2

Explanation: One island at top-left, one at bottom-right.

Constraints

  • O(M * N) time complexity.
GraphsGrid
JavaScript
Loading...
1 Hidden

Input Arguments

grid[["1","1","0"],["1","1","0"],["0","0","1"]]

Expected Output

2

Click RUN to test your solution