Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
Invariant: Each connected component of '1's is visited exactly once by flood fill.
Grid traversal can sink visited cells in-place to achieve O(1) auxiliary space without a visited matrix.
Bottleneck: Visits each cell at most twice.
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
Invariant: Each connected component of '1's is visited exactly once by flood fill.
Grid traversal can sink visited cells in-place to achieve O(1) auxiliary space without a visited matrix.
Bottleneck: Visits each cell at most twice.
Initialized 2D Grid map. Scanning for unvisited land cells ('1').