Skip to content

Commit 65e6547

Browse files
committedMay 16, 2023
✨ [q15] Runtime 161 ms Beats 99.8% Memory 59.3 MB Beats 24.86%
1 parent 3999b66 commit 65e6547

File tree

2 files changed

+41
-155
lines changed

2 files changed

+41
-155
lines changed
 

‎15/my_solution.js

+24-92
Original file line numberDiff line numberDiff line change
@@ -8,109 +8,41 @@ const threeSum = (nums) => {
88
console.log(nums)
99

1010
for (let i = 0; i < nums.length; i++) {
11-
if (undefined !== nums[i - 1] && nums[i - 1] === nums[i]) {
11+
if (i > 0 && nums[i] === nums[i - 1]) {
1212
console.log("-------------------> !!!! same as prev")
1313
console.log(nums[i - 1])
1414
console.log(nums[i])
1515
continue;
1616
}
1717

18-
let tmpList = [...nums];
19-
tmpList.splice(i, 1);
20-
21-
let b = (0 === nums[i]) ? 0 : -nums[i];
22-
let c = nums[i] + b;
23-
console.log(`i: ${i}, nums[i]: ${nums[i]}, b: ${b}, c: ${c}`);
24-
console.log("tmpList")
25-
console.log(tmpList)
26-
27-
// // Build a hash map except the current item in order to find the other 2 group mates.
28-
// for (let j = 0; j < tmpList.length; j++) {
29-
// tmpMap[tmpList[j]] = tmpList[j]
30-
// }
31-
// console.log(tmpMap)
32-
33-
result = findGroup(Math.min(...nums), Math.max(...nums), nums[i], b, c, tmpList, result);
18+
let l = i + 1, r = nums.length - 1;
19+
while (r > l) {
20+
let n = (nums[i] + nums[l] + nums[r]);
21+
console.log("n")
22+
console.log(n)
23+
24+
if (0 === n) {
25+
console.log(`Pushing ${i}`)
26+
result.push([nums[i], nums[l], nums[r]]);
27+
// r--;
28+
l++;
29+
while (nums[l] == nums[l - 1] && r > l) {
30+
l++;
31+
}
32+
} else if (n > 0) {
33+
r--;
34+
} else { // < 0
35+
l++;
36+
}
37+
}
3438
}
3539

3640
console.log("result")
3741
console.log(result)
38-
// console.log(removeDuplicates(result))
39-
// console.log([...new Set(result.map(JSON.stringify))].map(JSON.parse));
4042

41-
return removeDuplicates(result);
43+
return (result);
4244
};
4345

44-
function compareArrays(arr1, arr2) {
45-
if (arr1.length !== arr2.length) {
46-
return false;
47-
}
48-
49-
arr1.sort();
50-
arr2.sort();
51-
52-
for (let i = 0; i < arr1.length; i++) {
53-
if (arr1[i] !== arr2[i]) {
54-
return false;
55-
}
56-
}
57-
58-
return true;
59-
}
60-
61-
function removeDuplicates(list) {
62-
let uniqueSet = new Set();
63-
let uniqueList = [];
64-
65-
for (let i = 0; i < list.length; i++) {
66-
let isUnique = true;
67-
let array = list[i];
68-
69-
for (let j = 0; j < uniqueList.length; j++) {
70-
if (compareArrays(array, uniqueList[j])) {
71-
isUnique = false;
72-
break;
73-
}
74-
}
75-
76-
if (isUnique) {
77-
uniqueSet.add(JSON.stringify(array));
78-
uniqueList.push(array);
79-
}
80-
}
81-
82-
return uniqueList;
83-
}
84-
85-
const findGroup = (min, max, a, b, c, list, result) => {
86-
if (b > max || c < min) return result;
87-
88-
console.log(`-> min: ${min}, max: ${max}, a: ${a}, b: ${b}, c: ${c}, result: ${result}`);
89-
// let tmpMap = { ...map }, tmp = [a];
90-
let tmpList = [...list], tmp = [a];
91-
92-
if (tmpList.includes(b)) {
93-
console.log("--> tmpList[b] Exist!")
94-
console.log(b)
95-
tmp = [...tmp, b];
96-
97-
tmpList.splice(tmpList.indexOf(b), 1)
98-
99-
if (tmpList.includes(c)) {
100-
console.log("---> tmpList[c] Exist!")
101-
console.log(c)
102-
103-
result.push([...tmp, c])
104-
tmpList.splice(tmpList.indexOf(c), 1)
105-
106-
console.log("----> result");
107-
console.log(result);
108-
}
109-
}
110-
111-
return findGroup(min, max, a, b + 1, c - 1, list, result);
112-
113-
}
114-
11546
// threeSum([0, 0, 0])
116-
threeSum([-1, 0, 1, 2, -1, -4])
47+
// threeSum([-1, 0, 1, 2, -1, -4])
48+
threeSum([-2, 0, 0, 2, 2])

‎15/solution.js

+17-63
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,30 @@
44
*/
55
const threeSum = (nums) => {
66
let result = [];
7+
nums.sort((a, b) => a - b);
78

89
for (let i = 0; i < nums.length; i++) {
9-
let tmpList = [...nums];
10-
tmpList.splice(i, 1);
10+
if (i > 0 && nums[i] === nums[i - 1]) continue;
1111

12-
let b = (0 === nums[i]) ? 0 : -nums[i];
13-
let c = nums[i] + b;
12+
let l = i + 1, r = nums.length - 1;
1413

15-
result = findGroup(Math.min(...nums), Math.max(...nums), nums[i], b, c, tmpList, result);
16-
}
17-
18-
return removeDuplicates(result);
19-
};
20-
21-
function compareArrays(arr1, arr2) {
22-
if (arr1.length !== arr2.length) {
23-
return false;
24-
}
25-
26-
arr1.sort();
27-
arr2.sort();
28-
29-
for (let i = 0; i < arr1.length; i++) {
30-
if (arr1[i] !== arr2[i]) {
31-
return false;
32-
}
33-
}
34-
35-
return true;
36-
}
37-
38-
function removeDuplicates(list) {
39-
let uniqueSet = new Set();
40-
let uniqueList = [];
14+
while (r > l) {
15+
let n = (nums[i] + nums[l] + nums[r]);
4116

42-
for (let i = 0; i < list.length; i++) {
43-
let isUnique = true;
44-
let array = list[i];
17+
if (0 === n) {
18+
result.push([nums[i], nums[l], nums[r]]);
4519

46-
for (let j = 0; j < uniqueList.length; j++) {
47-
if (compareArrays(array, uniqueList[j])) {
48-
isUnique = false;
49-
break;
20+
l++;
21+
while (nums[l] == nums[l - 1] && r > l) {
22+
l++;
23+
}
24+
} else if (n > 0) {
25+
r--;
26+
} else {
27+
l++;
5028
}
5129
}
52-
53-
if (isUnique) {
54-
uniqueSet.add(JSON.stringify(array));
55-
uniqueList.push(array);
56-
}
5730
}
5831

59-
return uniqueList;
60-
}
61-
62-
const findGroup = (min, max, a, b, c, list, result) => {
63-
if (b > max || c < min) return result;
64-
65-
let tmpList = [...list], tmp = [a];
66-
67-
if (tmpList.includes(b)) {
68-
tmp = [...tmp, b];
69-
tmpList.splice(tmpList.indexOf(b), 1)
70-
71-
if (tmpList.includes(c)) {
72-
result.push([...tmp, c])
73-
tmpList.splice(tmpList.indexOf(c), 1)
74-
}
75-
}
76-
77-
return findGroup(min, max, a, b + 1, c - 1, list, result);
78-
79-
}
32+
return result;
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.