def smallestDifference(arrayOne, arrayTwo):
arrayOne.sort()
arrayTwo.sort()
smallest, pair = float("inf"), []
oneIdx, twoIdx = 0, 0
while (oneIdx < len(arrayOne) and twoIdx < len(arrayTwo)):
numOne, numTwo = arrayOne[oneIdx], arrayTwo[twoIdx]
curDiff = abs(numOne - numTwo)
if (curDiff < smallest):
smallest = curDiff
pair = [numOne, numTwo]
if numOne > numTwo:
twoIdx += 1
else:
oneIdx += 1
return pair
'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글
Kattis Climbing Worm (0) | 2022.07.20 |
---|---|
Kattis Average Character (0) | 2022.07.20 |
Kattis Sort (0) | 2022.07.20 |
Kattis Skru op! (0) | 2022.07.19 |
AlgoExpert Three Number Sum (0) | 2022.07.19 |