Skip to content

Commit b5e5bce

Browse files
author
Мето Трајковски
committed
Added new solutions
1 parent 4f77335 commit b5e5bce

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lists/find_all_intervals.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'''
2+
Find All Intervals
3+
4+
You are given an array of intervals.
5+
Each interval is defined as: (start, end). e.g. (2, 5)
6+
It represents all the integer numbers in the interval, including start and end. (in the example 2, 3, 4 and 5).
7+
Given the array of intervals find the smallest set of unique intervals that contain the same integer numbers, without overlapping.
8+
9+
10+
Input: [(1, 5), (2, 6)]
11+
Output: [(1, 6)]
12+
13+
Input: [(2, 4), (5, 5), (6, 8)]
14+
Output: [(2, 8)]
15+
16+
Input: [(1, 4), (6, 9), (8, 10)]
17+
Output: [(1, 4), (6, 10)]
18+
19+
=========================================
20+
Sort the intervals (using the start), accessing order. After that just iterate the intervals
21+
and check if the current interval belongs to the last created interval.
22+
Time Complexity: O(N LogN)
23+
Space Complexity: O(1)
24+
'''
25+
26+
27+
############
28+
# Solution #
29+
############
30+
31+
def find_all_intervals(intervals):
32+
n = len(intervals)
33+
if n == 0:
34+
return []
35+
36+
# sort the intervals
37+
intervals.sort(key=lambda interval: interval[0])
38+
result = []
39+
start, end = intervals[0]
40+
41+
for i in range(1, n):
42+
# 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])
46+
else:
47+
# save this interval
48+
result.append((start, end))
49+
# create a new interval
50+
start, end = intervals[i]
51+
52+
# add the last interval
53+
result.append((start, end))
54+
55+
return result
56+
57+
58+
###########
59+
# Testing #
60+
###########
61+
62+
# Test 1
63+
# Correct result => [(1, 6)]
64+
print(find_all_intervals([(1, 5), (2, 6)]))
65+
66+
# Test 2
67+
# Correct result => [(2, 8)]
68+
print(find_all_intervals([(2, 4), (5, 5), (6, 8)]))
69+
70+
# Test 3
71+
# Correct result => [(1, 4), (6, 10)]
72+
print(find_all_intervals([(1, 4), (6, 9), (8, 10)]))

Lists/min_swaps.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'''
2+
Min Swaps
3+
4+
You have a list of numbers and you want to sort the list.
5+
The only operation you have is a swap of any two arbitrary numbers.
6+
Find the minimum number of swaps you need to do in order to make the list sorted (ascending order).
7+
- The array will contain N elements
8+
- Each element will be between 1 and N inclusive
9+
- All the numbers will be different
10+
11+
Input: [4, 1, 3, 2]
12+
Output: 2
13+
Output explanation: swap(4, 1) = [1, 4, 3, 2], swap(4, 2) = [1, 2, 3, 4]
14+
15+
=========================================
16+
According to the description, all elements will have their position in the array,
17+
for example, K should be located at K-1 in the array.
18+
Itterate the array and check if each position has the right element,
19+
if not, put that element in the right position and check again.
20+
Time Complexity: O(N) , the solution looks like O(N^2) but that's not possible, at most O(2*N) operations can be done
21+
Space Complexity: O(1)
22+
'''
23+
24+
25+
############
26+
# Solution #
27+
############
28+
29+
def min_swaps(a):
30+
n = len(a)
31+
swaps = 0
32+
33+
for i in range(n):
34+
# swap the elements till the right element isn't found
35+
while a[i] - 1 != i:
36+
# swap the elements
37+
tmp = a[a[i] - 1]
38+
a[a[i] - 1] = a[i]
39+
a[i] = tmp
40+
41+
swaps += 1
42+
43+
return swaps
44+
45+
46+
###########
47+
# Testing #
48+
###########
49+
50+
# Test 1
51+
# Correct result => 2
52+
print(min_swaps([4, 1, 3, 2]))
53+
54+
# Test 2
55+
# Correct result => 3
56+
print(min_swaps([4, 1, 2, 3]))

0 commit comments

Comments
 (0)