File tree 1 file changed +13
-22
lines changed
1 file changed +13
-22
lines changed Original file line number Diff line number Diff line change 1
- # https://www.algoexpert.io/questions/Smallest%20Difference
2
-
3
1
# 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 = []
12
7
while index_one < len (array_one ) and index_two < len (array_two ):
13
8
first_num = array_one [index_one ]
14
9
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
27
18
return result_pair
28
19
29
20
You can’t perform that action at this time.
0 commit comments