Skip to content

Commit 8383c07

Browse files
author
Mauricio Klein
committed
Add solution for challenge #54
1 parent 139d02e commit 8383c07

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353
- [x] [Challenge 51: Subsequences of string](challenge-51/)
5454
- [x] [Challenge 52: Deepest Node in a Binary Tree](challenge-52/)
5555
- [x] [Challenge 53: Thousand missing numbers](challenge-53/)
56+
- [x] [Challenge 54: Find the number of islands](challenge-54/)

challenge-54/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
python test_*.py
3+
4+
.PHONY: test

challenge-54/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Find the number of islands
2+
3+
This problem was asked by Linkedin.
4+
5+
## Description
6+
7+
Given a 2-dimensional grid consisting of 1's (land blocks) and 0's (water blocks), count the number of islands present in the grid.
8+
9+
The definition of an island is as follows:
10+
11+
1. Must be surrounded by water blocks.
12+
2. Consists of land blocks (1's) connected to adjacent land blocks (either vertically or horizontally).
13+
14+
Assume all edges outside of the grid are water.
15+
16+
## Example
17+
18+
```
19+
Input:
20+
10001
21+
11000
22+
10110
23+
00000
24+
25+
Output: 3
26+
```

challenge-54/__init__.py

Whitespace-only changes.

challenge-54/solver.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#
3+
# Time complexity : O(n*m) (where n = rows in the grid, m = columns in the grid)
4+
# Space complexity: O(n*m) (worst case, when all the positions are land and the walk goes recursively to all positions)
5+
#
6+
def count_islands(grid):
7+
rows, columns = len(grid), len(grid[0])
8+
islands = 0
9+
10+
for row in range(rows):
11+
for column in range(columns):
12+
if grid[row][column] == 1:
13+
islands += 1
14+
walk(grid, (row, column), (rows, columns))
15+
16+
return islands
17+
18+
#
19+
# walk marks the cell (pos) + all non-visited neighbours
20+
# as visited (cell value = 2)
21+
#
22+
def walk(grid, pos, dimensions):
23+
r, c = pos
24+
rows, columns = dimensions
25+
26+
def inbounds(row, column):
27+
return (0 <= row < rows) and (0 <= column < columns)
28+
29+
# Mark the current position as visited
30+
grid[r][c] = 2
31+
32+
# Mark the non-visited land neighbours as visited
33+
paths = [(-1,0), (1,0), (0,-1), (0,1)]
34+
35+
for path in paths:
36+
nr = r + path[0]
37+
nc = c + path[1]
38+
39+
if inbounds(nr, nc) and grid[nr][nc] == 1:
40+
walk(grid, (nr, nc), dimensions)

challenge-54/test_solver.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import unittest, random
2+
from solver import count_islands
3+
4+
class TestSolver(unittest.TestCase):
5+
def test_count_islands(self):
6+
grid = [
7+
[1,0,0,0,1],
8+
[1,1,0,0,0],
9+
[1,0,1,1,0],
10+
[0,0,0,0,0]
11+
]
12+
self.assertEqual(count_islands(grid), 3)
13+
14+
grid = [
15+
[1,0,0,0,1],
16+
[1,1,0,1,1],
17+
[1,1,0,1,1],
18+
[1,0,0,0,1]
19+
]
20+
self.assertEqual(count_islands(grid), 2)
21+
22+
grid = [
23+
[1,0,0,0,1],
24+
[0,1,0,1,0],
25+
[0,1,0,1,0],
26+
[1,0,0,0,1]
27+
]
28+
self.assertEqual(count_islands(grid), 6)
29+
30+
grid = [
31+
[0,0,0,0,0],
32+
[0,1,1,1,0],
33+
[0,1,1,1,0],
34+
[0,0,0,0,0]
35+
]
36+
self.assertEqual(count_islands(grid), 1)
37+
38+
grid = [
39+
[0,0,0,0,0],
40+
[0,0,0,0,0],
41+
[0,0,0,0,0],
42+
[0,0,0,0,0]
43+
]
44+
self.assertEqual(count_islands(grid), 0)
45+
46+
grid = [
47+
[1,1,1,1,1],
48+
[1,1,1,1,1],
49+
[1,1,1,1,1],
50+
[1,1,1,1,1]
51+
]
52+
self.assertEqual(count_islands(grid), 1)
53+
54+
if __name__ == "__main__":
55+
unittest.main()

0 commit comments

Comments
 (0)