Skip to content

Commit ece3383

Browse files
committed
added Number of Islands (Medium)
1 parent 55c4c62 commit ece3383

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

Medium/NumberOfIslands/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Number of Islands
2+
3+
[Leetcode Link](https://leetcode.com/problems/number-of-islands/)
4+
5+
## Problem:
6+
7+
Given an `m x n` 2d grid map of `'1'`s (land) and `'0'`s (water), return the number of islands.
8+
9+
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.
10+
11+
## Example:
12+
13+
```
14+
Input: grid = [
15+
["1","1","1","1","0"],
16+
["1","1","0","1","0"],
17+
["1","1","0","0","0"],
18+
["0","0","0","0","0"]
19+
]
20+
Output: 1
21+
```
22+
23+
```
24+
Input: grid = [
25+
["1","1","0","0","0"],
26+
["1","1","0","0","0"],
27+
["0","0","1","0","0"],
28+
["0","0","0","1","1"]
29+
]
30+
Output: 3
31+
```
32+
33+
## Note:
34+
35+
- m == grid.length
36+
- n == grid[i].length
37+
- 1 <= m, n <= 300
38+
- grid[i][j] is '0' or '1'.

Medium/NumberOfIslands/solution.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def numIslands(self, grid: List[List[str]]) -> int:
6+
count = 0
7+
for i in range(0, len(grid)):
8+
for j in range(0, len(grid[i])):
9+
if grid[i][j] == "1":
10+
count += 1
11+
self.checkIslandDFS(grid, i, j)
12+
return count
13+
14+
def checkIslandDFS(self, grid, i, j):
15+
# base case
16+
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[i]) or grid[i][j] == "0":
17+
return
18+
19+
grid[i][j] = "0"
20+
# check all adjacents
21+
self.checkIslandDFS(grid, i, j-1) # left
22+
self.checkIslandDFS(grid, i, j+1) # right
23+
self.checkIslandDFS(grid, i-1, j) # top
24+
self.checkIslandDFS(grid, i+1, j) # bottom
25+
26+
27+
sol = Solution()
28+
# grid = [
29+
# ["1", "1", "1", "1", "0"],
30+
# ["1", "1", "0", "1", "0"],
31+
# ["1", "1", "0", "0", "0"],
32+
# ["0", "0", "0", "0", "0"]
33+
# ]
34+
# print(sol.numIslands(grid))
35+
36+
# grid = [
37+
# ["1", "1", "0", "0", "0"],
38+
# ["1", "1", "0", "0", "0"],
39+
# ["0", "0", "1", "0", "0"],
40+
# ["0", "0", "0", "1", "1"]
41+
# ]
42+
grid = [["1", "1", "1"], ["0", "1", "0"], ["1", "1", "1"]]
43+
print(sol.numIslands(grid))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Languages used: Java and Python
8585
- [Score After Flipping Matrix](Medium/ScoreAfterFlippingMatrix)
8686
- [Construct Binary Tree from Preorder and Inorder Traversal](Medium/ConstructBinaryTree)
8787
- [K Closest Points to Origin](Medium/KClosestPointsToOrigin)
88+
- [Number of Islands](Medium/NumberOfIslands)
8889
- Hard
8990
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
9091
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)