-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.js
More file actions
52 lines (45 loc) · 1.22 KB
/
Copy paththreeSum.js
File metadata and controls
52 lines (45 loc) · 1.22 KB
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
/**
* https://leetcode.com/problems/3sum
* Solution using sorting and 2Sum subproblems
* Time complexity - O(n^2)
* Space complexity - O(n)
* @param {number[]} nums
* @return {number[][]}
*/
function threeSum(nums) {
nums.sort((a, b) => a - b);
const result = [];
for (let L = 0; L < nums.length && nums[L] <= 0; L++) {
if (L == 0 || nums[L] !== nums[L - 1]) {
let M = L + 1, R = nums.length - 1;
while (M < R) {
const sum = nums[L] + nums[M] + nums[R];
if (sum > 0) {
R--;
} else if (sum < 0) {
M++;
} else {
result.push([nums[L], nums[M++], nums[R--]]);
while (M < R && nums[M] == nums[M - 1])
M++;
}
}
}
}
return result;
};
// Cases:
// [0,0,0]
// [-1,0,0,2]
// [-4,-1,-1,0]
// [-4,-1,-1,0,1,2,4]
// [-4,-1,-1,0,0,0,1,2,7,9,10,20]
// Breaking down the problem:
// When to update pointers:
// If sum(L, M, R) >= 0 -> decrement R
// If sum(L, M, R) < 0 -> increment M
// Base cases for M:
// M < R
// Base cases for L:
// L > 0
module.exports = threeSum;