Link: https://leetcode.com/problems/number-of-islands/
Solution:
Intuition
Very easy graph problem. My approach was just to iterate over each cell and if the value was 1
, increment the result and the do a DFS on that cell to find all its neighbouring ones and set them to zeros so that we don’t consider them again. So basically in in-place transformation of the grid to avoid using a visited
set.
Pretty much a classic connected component problem.
Implementation
Review 1
Don’t forget about in-place transformation instead of using a visited set.