-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-3SumClosest.js
120 lines (104 loc) · 3.63 KB
/
16-3SumClosest.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var threeSumClosest = function (nums, target) {
let mergeSort = function (array) {
if (array.length === 0) {
return [];
}
let merge = function (firstArray, secondArray) {
let i = 0;
let j = 0;
let result = [];
while (true) {
if (firstArray[i] < secondArray[j]) {
result.push(firstArray[i]);
i++;
} else {
result.push(secondArray[j]);
j++;
}
if (i === firstArray.length) {
return result.concat(
secondArray.slice(j, secondArray.length)
);
} else if (j === secondArray.length) {
return result.concat(
firstArray.slice(i, firstArray.length)
);
}
}
};
if (array.length === 1) {
return array;
}
const middleIndex = Math.floor(array.length / 2);
const firstArray = mergeSort(array.slice(0, middleIndex));
const secondArray = mergeSort(array.slice(middleIndex, array.length));
const merged = merge(firstArray, secondArray);
return merged;
};
const twoSumClosest = function (sorted, target) {
if (sorted.length < 2) {
return [];
}
let i = 0;
let j = sorted.length - 1;
let minSum = sorted[i] + sorted[j];
let distance = Math.abs(sorted[i] + sorted[j] - target);
while (true) {
sum = sorted[i] + sorted[j];
if (Math.abs(sum - target) < distance) {
distance = Math.abs(sum - target);
minSum = sum;
}
// console.log(sorted[i], sorted[j]);
// console.log("sum", sum);
// console.log("distance", distance);
// console.log("minSum", sum);
if (sum > target) {
if (j - 1 <= i) {
return minSum;
}
j--;
} else if (sum < target) {
// console.log("sum < target");
if (i + 1 >= j) {
return minSum;
}
i++;
} else {
// console.log("sum = target");
return minSum;
}
}
};
const sorted = mergeSort(nums);
console.log("sorted", sorted);
let minDistance = undefined;
let closestSum = undefined;
for (let i = 0; i < sorted.length; i++) {
let first = sorted[i];
let arr = sorted.slice(i + 1);
if (arr.length >= 2) {
console.log("first", first);
console.log("arr", arr);
console.log("target", target - first);
let sum = first + twoSumClosest(arr, target - first);
console.log("twoSumClosest", twoSumClosest(arr, target - first));
console.log("sum", sum);
if (!closestSum && closestSum !== 0) {
closestSum = sum;
}
if (!minDistance && minDistance !== 0) {
minDistance = Math.abs(sum - target);
}
console.log("distance", Math.abs(sum - target));
if (Math.abs(sum - target) < minDistance) {
minDistance = Math.abs(sum - target);
closestSum = sum;
}
console.log("minDistance", minDistance);
console.log("closestSum", closestSum);
}
}
return closestSum;
};
console.log(threeSumClosest([1,2,5,10,11], 12));