Skip to content

Commit 0237794

Browse files
Meto TrajkovskiMeto Trajkovski
authored andcommitted
Added 2 new solutions
1 parent 9a03843 commit 0237794

2 files changed

Lines changed: 97 additions & 18 deletions

File tree

Backtracking/number_of_islands.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'''
2+
Number of Islands
3+
4+
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
5+
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
6+
You may assume all four edges of the grid are all surrounded by water.
7+
8+
Input: [
9+
['1','1','1','1','0'],
10+
['1','1','0','1','0'],
11+
['1','1','0','0','0'],
12+
['0','0','0','0','0']
13+
]
14+
Output: 1
15+
16+
Input: [
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+
This problem can be solved in several ways (using DFS recursion or using the stack data structure) i'll solve it with BFS using Queue data structure.
26+
Time Complexity: O(M * N)
27+
Space Complexity: O(M * N)
28+
'''
29+
30+
31+
############
32+
# Solution #
33+
############
34+
35+
from collections import deque
36+
37+
def num_of_islands(grid):
38+
n = len(grid)
39+
if n == 0:
40+
return 0
41+
m = len(grid[0])
42+
43+
islands = 0
44+
queue = deque()
45+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
46+
47+
for i in range(n):
48+
for j in range(m):
49+
# search for an island
50+
if grid[i][j] == '1':
51+
islands += 1
52+
queue.append((i, j))
53+
54+
# BFS
55+
while queue:
56+
coord = queue.popleft()
57+
x, y = coord
58+
59+
if grid[x][y] != '1':
60+
continue
61+
# delete the island
62+
grid[x][y] = '0'
63+
64+
for direction in directions:
65+
# calculate the next position
66+
next_x, next_y = (x + direction[0], y + direction[1])
67+
# check if the next position is valid
68+
if (next_x < 0) or (next_x >= n):
69+
continue
70+
if (next_y < 0) or (next_y >= m):
71+
continue
72+
# save this position
73+
queue.append((next_x, next_y))
74+
75+
return islands
76+
77+
78+
###########
79+
# Testing #
80+
###########
81+
82+
# Test 1
83+
# Correct result => 3
84+
print(num_of_islands([['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1', '1']]))
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Find All Intervals
2+
Merge Intervals
33
44
You are given an array of intervals.
55
Each interval is defined as: (start, end). e.g. (2, 5)
@@ -20,39 +20,34 @@
2020
Sort the intervals (using the start), accessing order. After that just iterate the intervals
2121
and check if the current interval belongs to the last created interval.
2222
Time Complexity: O(N LogN)
23-
Space Complexity: O(1)
23+
Space Complexity: O(N) , for the result
2424
'''
2525

2626

2727
############
2828
# Solution #
2929
############
3030

31-
def find_all_intervals(intervals):
31+
def merge_intervals(intervals):
3232
n = len(intervals)
3333
if n == 0:
3434
return []
3535

3636
# sort the intervals
3737
intervals.sort(key=lambda interval: interval[0])
38-
result = []
39-
start, end = intervals[0]
38+
mergedIntervals = []
39+
mergedIntervals.append(intervals[0])
4040

4141
for i in range(1, n):
4242
# check if this interval belongs to the last created interval
43-
if intervals[i][0] <= end + 1:
44-
# only the end can be changed (start is min, because array is sorted)
45-
end = max(end, intervals[i][1])
43+
if intervals[i][0] <= mergedIntervals[-1][1] + 1:
44+
# only the end can be changed (just copy start it's min, because the array is sorted)
45+
mergedIntervals[-1] = (mergedIntervals[-1][0], max(mergedIntervals[-1][1], intervals[i][1]))
4646
else:
47-
# save this interval
48-
result.append((start, end))
4947
# create a new interval
50-
start, end = intervals[i]
48+
mergedIntervals.append(intervals[i])
5149

52-
# add the last interval
53-
result.append((start, end))
54-
55-
return result
50+
return mergedIntervals
5651

5752

5853
###########
@@ -61,12 +56,12 @@ def find_all_intervals(intervals):
6156

6257
# Test 1
6358
# Correct result => [(1, 6)]
64-
print(find_all_intervals([(1, 5), (2, 6)]))
59+
print(merge_intervals([(1, 5), (2, 6)]))
6560

6661
# Test 2
6762
# Correct result => [(2, 8)]
68-
print(find_all_intervals([(2, 4), (5, 5), (6, 8)]))
63+
print(merge_intervals([(2, 4), (5, 5), (6, 8)]))
6964

7065
# Test 3
7166
# Correct result => [(1, 4), (6, 10)]
72-
print(find_all_intervals([(1, 4), (6, 9), (8, 10)]))
67+
print(merge_intervals([(1, 4), (6, 9), (8, 10)]))

0 commit comments

Comments
 (0)