Question
Given an
m x n2D binary gridgridwhich represents a map of'1's (land) and'0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Ideas
- DS: Adjacency list
- Traversal: DFS
- Here DFS doesn’t actually contribute to our result, but serves as a mechanism to mark the entire island
- We want to iterate over our entire matrix (m*n) and if we see a “1”, call dfs to mark all adjacent bodies as “seen”, or part of the island
- In DFS, we just want to traverse all neighbouring 1’s