-
Notifications
You must be signed in to change notification settings - Fork 279
/
16 3Sum Closest.js
79 lines (66 loc) · 1.8 KB
/
16 3Sum Closest.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
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
if(nums === null || nums.length < 2){
return null;
}
if(nums.length === 3){
return nums.reduce(function(prev,cur){return prev + cur;});
}
var result = 0;
var closest = Infinity;
nums.sort(function(a,b){return a > b ? 1 : -1;});
for(var i = 0; i < nums.length; i++){
var j = i + 1;
var k = nums.length - 1;
while(j < k){
var sum = nums[j] + nums[k] + nums[i];
var diff = sum - target;
if(diff === 0){
return sum;
}
if(sum < target){
diff = target - sum;
j++;
} else {
diff = sum - target;
k--
}
if(diff < closest){
closest = diff;
result = sum;
}
}
while(i < (nums.length-1) && nums[i] === nums[i+1]) i++;
}
return result;
};
//Shorter solution
var threeSumClosest = function(nums, target) {
var closest = Number.Infinity;
var gap = -1;
nums.sort(function(a, b) { return a - b });
for(var i = 0; i < nums.length - 2; i++) {
var low = i + 1;
var high = nums.length - 1;
while(low < high) {
var sum = nums[i] + nums[low] + nums[high];
partialGap = Math.abs(target - sum);
if(partialGap < gap || gap === -1) {
gap = partialGap;
closest = sum;
}
if(sum === target) {
return target;
} else if (sum < target) {
low++;
} else {
high--;
}
}
}
return closest;
};