-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallestDifference.js
62 lines (49 loc) · 1.43 KB
/
SmallestDifference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'
/*
Given two arrays of integers, compute the pair of values (one value in each array) with the smallest(non-negative) difference. Return the difference.
EXAMPLE
Input: [1, 3, 15, 11, 2], [23, 127, 235, 19, 8]
Output: 3. That is, the pair(11, 8)
*/
class SmallestDifferenceSlower{
/*
Time complexity: O(m*n)
*/
findSmallestDifference(list1, list2){
if (list1.length === 0 || list2.length === 0) return -1
let min = Number.MAX_SAFE_INTEGER
list1.forEach(element1 => {
list2.forEach(element2 => {
if(Math.abs(element1 - element2) < min){
min = Math.abs(element1 - element2)
}
})
});
return min
}
}
class SmallestDifference{
/*
Time complexity = O(m*logm + n*logn)
*/
findSmallestDifference(list1, list2){
list1.sort((a, b) => a - b)
list2.sort((a, b) => a - b)
let a = 0,
b = 0
let difference = Number.MAX_SAFE_INTEGER
while(a < list1.length && b < list2.length){
if (Math.abs(list1[a] - list2[b]) < difference){
difference = Math.abs(list1[a] - list2[b])
}
if (list1[a] < list2[b]){
a += 1
}
else{
b += 1
}
}
return difference
}
}
module.exports = SmallestDifference