GGoogle·DSAL4Onsite – Coding 1
Number of Islands
Problem
Given an m × n binary grid where "1" is land and "0" is water, return the number of islands.
An island is surrounded by water and formed by connecting adjacent land cells horizontally or vertically.
Example
grid = [
["1","1","0","0"],
["1","1","0","0"],
["0","0","1","0"],
["0","0","0","1"]
]
Output: 3
Follow-ups Google commonly asks
- What if the grid is too large to fit in memory — it streams row by row?
- Solve using Union-Find instead of DFS/BFS
- Count islands where diagonal connections also count
added 6 days ago