Skip to content

Commit 08a8c3b

Browse files
Added 3 Sum Closest
1 parent d06ba69 commit 08a8c3b

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
tips
22
.DS_Store
33

4-
node_modules/
4+
node_modules/
5+
6+
JAVASCRIPT.md
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
3Sum Closest
3+
https://leetcode.com/problems/3sum-closest/
4+
5+
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
6+
7+
Return the sum of the three integers.
8+
9+
You may assume that each input would have exactly one solution.
10+
11+
Example 1:
12+
13+
Input: nums = [-1,2,1,-4], target = 1
14+
Output: 2
15+
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
16+
Example 2:
17+
18+
Input: nums = [0,0,0], target = 1
19+
Output: 0
20+
21+
22+
Constraints:
23+
24+
3 <= nums.length <= 1000
25+
-1000 <= nums[i] <= 1000
26+
-104 <= target <= 104
27+
*/
28+
/**
29+
* @param {number[]} nums
30+
* @param {number} target
31+
* @return {number}
32+
*/
33+
var threeSumClosest = function(nums, target) {
34+
let mid = 1;
35+
let right = nums.length - 1;
36+
let currentSum = nums[0] + nums[mid] + nums[right];
37+
let closest = currentSum;
38+
39+
nums.sort(function(a,b) {return a - b})
40+
41+
for(var left = 0 ; left < nums.length - 1; left++) {
42+
mid = left + 1;
43+
right = nums.length - 1;
44+
45+
while(mid < right) {
46+
currentSum = nums[left] + nums[mid] + nums[right];
47+
48+
if(Math.abs(target - currentSum) < Math.abs(target - closest)) {
49+
closest = currentSum;
50+
}
51+
52+
if(currentSum > target) {
53+
right--;
54+
} else {
55+
mid++;
56+
}
57+
}
58+
}
59+
60+
return closest;
61+
};
62+
63+
module.exports.threeSumClosest = threeSumClosest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const assert = require('assert');
2+
const threeSumClosest = require('../../LeetcodeProblems/Algorithms/3SumClosest').threeSumClosest;
3+
4+
var test = function () {
5+
assert.equal(2, threeSumClosest([-1, 2 , 1, -4], 1));
6+
assert.equal(0, threeSumClosest([0, 0, 0], 1));
7+
8+
}
9+
10+
module.exports.test = test;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
2323
| [merge k sorted lists ](/LeetcodeProblems/Algorithms/merge_k_sorted_lists.js) | Hard | https://leetcode.com/problems/merge-k-sorted-lists/ |
2424
| [Set Matrix Zeroes](/LeetcodeProblems/Algorithms/Set_Matrix_Zeroes.js) | Hard | https://leetcode.com/problems/set-matrix-zeroes/ |
2525
| [Subarray Sum Equals K ](/LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K.js) | Medium | https://leetcode.com/problems/subarray-sum-equals-k/ |
26+
| [3Sum Closest](/LeetcodeProblems/Algorithms/3SumClosest.js) | Medium | https://leetcode.com/problems/3sum-closest/ |
2627
| [3Sum ](/LeetcodeProblems/Algorithms/3Sum.js) | Medium | https://leetcode.com/problems/3sum/ |
2728
| [NumberOfIslands ](/LeetcodeProblems/Algorithms/Number_of_Islands.js) | Medium | https://leetcode.com/problems/number-of-islands/ |
2829
| [Swap Nodes in Pairs](/LeetcodeProblems/Algorithms/Swap_Nodes_in_Pairs.js) | Medium | https://leetcode.com/problems/swap-nodes-in-pairs/ |

0 commit comments

Comments
 (0)