Number of Islands
Graph
medium
Score: 40
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.
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.
Input Format:
2D Binary Grid ‘grid’
Output Format:
Return a single integer ‘the number of islands’
Example 1
Input :
4 5
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0
Output :
1
Example 2
Input :
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
Output :
3
Constraints:-
- m == grid.length .i.e Number of rows in grid
- n == grid[i].length i.e Number of columns in grid
- 1 <= m, n <= 300
- grid[i][j] is 0 or 1.
- Expected Time Complexity: O(n*m)
- Expected Space Complexity: O(n*m)