Skip to content

Commit 5a964e7

Browse files
committed
Number of Islands
1 parent 1ca0e7d commit 5a964e7

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Graphs/200-Number-of-Islands.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
'''
2+
Leetcode- https://leetcode.com/problems/number-of-islands/
3+
'''
4+
5+
'''
6+
200. Number of Islands
7+
8+
Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water),
9+
return the number of islands.
10+
11+
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
12+
You may assume all four edges of the grid are all surrounded by water.
13+
14+
Example 1:
15+
16+
Input: grid = [
17+
["1","1","0","0","0"],
18+
["1","1","0","0","0"],
19+
["0","0","1","0","0"],
20+
["0","0","0","1","1"]
21+
]
22+
Output: 3
23+
'''
24+
25+
class Solution:
26+
def numIslands(self, grid: List[List[str]]) -> int:
27+
if not grid:
28+
return 0
29+
island = 0
30+
noRows = len(grid)
31+
noCols = len(grid[0])
32+
for row in range(noRows):
33+
for col in range(noCols):
34+
if grid[row][col] == "1":
35+
self.dfs(grid, row, col)
36+
island+=1
37+
return island
38+
39+
def dfs(self,grid,row,col):
40+
if row<0 or row>=len(grid) or col<0 or col>=len(grid[0]) or grid[row][col]=="0":
41+
return
42+
43+
grid[row][col] = "0"
44+
45+
self.dfs(grid,row-1,col)
46+
self.dfs(grid,row+1,col)
47+
self.dfs(grid,row,col-1)
48+
self.dfs(grid,row,col+1)
49+
50+
'''
51+
Explanation:
52+
53+
1.Initialize island to 0.
54+
2.Calculate noRows (number of rows) and noCols (number of columns) of the grid.
55+
3.Start looping through each cell in the grid using nested loops.
56+
4.For each cell, if the cell contains "1", call the dfs function to explore the island.
57+
5.Inside the dfs function, check if the current cell is out of bounds or if it's water ("0"). If either condition is met, return from the function.
58+
6.Mark the current cell as visited by changing its value to "0".
59+
7.Recursively call dfs on the neighboring cells (up, down, left, right).
60+
8.Repeat steps 5-7 until all connected land cells are visited and marked as "0". This essentially explores and marks an entire island.
61+
9.Go back to the main function, increment the island count by 1.
62+
R10.epeat steps 4-9 for all cells in the grid.
63+
64+
65+
Now, let's dry run the code step by step using the provided input grid:
66+
67+
1.Initialize island to 0.
68+
2.Calculate noRows = 4 and noCols = 5.
69+
70+
Start looping through each cell in the grid:
71+
72+
- Cell at (0, 0) contains "1":
73+
74+
- Call dfs at (0, 0).
75+
-- Mark (0, 0) as "0".
76+
-- Call dfs at (0, -1), which returns immediately.
77+
-- Call dfs at (0, 1), which recursively calls dfs on (0, 2) and (1, 1), both return immediately.
78+
-- Call dfs at (-1, 0), which returns immediately.
79+
-- Call dfs at (1, 0), which recursively calls dfs on (2, 0), which calls dfs on (2, 1), (3, 0), and (1, 0), all return immediately.
80+
-- Call dfs at (0, 0) again (through recursion), which returns immediately.
81+
- Increment island to 1.
82+
83+
- Cell at (0, 1) contains "1":
84+
85+
- Call dfs at (0, 1), which returns immediately.
86+
- Continue this process for all cells in the grid.
87+
88+
Final island count: 3
89+
'''

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion
3939
- [x] [Find the Duplicate Number](Two-Pointers/287-Find-the-Duplicate-Number.py)
4040
- [x] [Subarray Product Less Than K](Two-Pointers/713-Subarray-Product-Less-Than-K.py)
4141

42+
- [x] [Graphs](Graphs)
43+
- [x] [Number of Islands](Graphs/200-Number-of-Islands.py)
44+

0 commit comments

Comments
 (0)