Skip to content

Commit de2ce65

Browse files
author
Partho Biswas
committed
no message
1 parent e7dc1f2 commit de2ce65

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed
+13-22
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
# https://www.algoexpert.io/questions/Smallest%20Difference
2-
31
# O(nlog(n) + mlon(m)) time | O(1) space
4-
def smallest_difference(array_one, array_two):
5-
array_one.sort()
6-
array_two.sort()
7-
result_pair = []
8-
index_one = 0
9-
index_two = 0
10-
smallest = float("inf")
11-
current = float("inf")
2+
def smallestDifference(arrayOne, arrayTwo):
3+
array_one, array_two = sorted(arrayOne), sorted(arrayTwo)
4+
index_one, index_two = 0, 0
5+
smallestDiff = float("inf")
6+
result_pair = []
127
while index_one < len(array_one) and index_two < len(array_two):
138
first_num = array_one[index_one]
149
second_num = array_two[index_two]
15-
if first_num < second_num:
16-
current = second_num - first_num
17-
index_one += 1
18-
elif second_num < first_num:
19-
current = first_num - second_num
20-
index_two += 1
21-
else:
22-
return [first_num, second_num]
23-
24-
if smallest > current:
25-
smallest = current
26-
result_pair = [first_num, second_num]
10+
currentDiff = abs(first_num - second_num)
11+
if currentDiff < smallestDiff:
12+
smallestDiff = currentDiff
13+
result_pair = [first_num, second_num]
14+
if first_num <= second_num:
15+
index_one += 1
16+
else:
17+
index_two += 1
2718
return result_pair
2819

2920

0 commit comments

Comments
 (0)