-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path3-Sum-Combined-Performance-Test.js
executable file
·133 lines (95 loc) · 3.78 KB
/
3-Sum-Combined-Performance-Test.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
121
122
123
124
125
126
127
128
129
130
131
132
/* https://leetcode.com/problems/3sum/description/ */
var threeSum_Brute = function(nums) {
nums = nums.sort(function (a, b) {
return a - b;
});
let uniqueTriplets = [];
let i, j, k;
let len = nums.length;
for (i = 0; i < len; i++) {
if (i > 0 && nums[i] === nums[i-1]) continue; // The continue statement "jumps over" one iteration in the loop. So, if 2 successive elements are same (i.e. duplicates) leave this iteration and jump over to the next one
for (j = i + 1; j < len; j++) {
if ( j > i + 1 && nums[j] === nums[j-1]) continue;
for (k = j + 1; k < len; k++) {
if (k > j + 1 && nums[k] === nums[k - 1]) continue;
if ((nums[i] + nums[j] + nums[k]) === 0) {
uniqueTriplets.push([nums[i], nums[j], nums[k]]);
// Very imp - I am wrapping individual elements nums[i], nums[j], nums[k] into a wrapper-array in the .push function. So that the final output comes in multiple array as required by the original problem statement.
}
}
}
}
return uniqueTriplets;
}
// Solution-2 - 2-Pointer solution, but less optimized than the 3rd solution below.
function threeSum2(arr) {
arr.sort((a, b) => {
return a - b;
});
const result = [];
for (let indexA = 0; indexA < arr.length - 2; indexA++) {
// if (arr[indexA] > 0) return result;
let indexB = indexA + 1;
let indexC = arr.length - 1;
if (indexA > 0 && arr[indexA] === arr[indexA - 1]) continue;
while (indexB < indexC ) {
let sum = arr[indexA] + arr[indexB] + arr[indexC];
if (sum < 0) {
indexB++;
} else if (sum > 0) {
indexC--;
} else {
result.push([arr[indexA], arr[indexB], arr[indexC]]);
while (arr[indexB] === arr[indexB + 1]) indexB++;
while (arr[indexC] === arr[indexC - 1]) indexC--
indexB++;
indexC--;
}
}
}
return result;
}
/* Solution-3- FASTEST "2 pointer solution" in O(n^2) Time */
var threeSumTwoPointer = function (nums) {
nums.sort((a, b) => a - b);
const result = [];
if (!nums || nums.length < 3) return result;
for (let indexA = 0; indexA < nums.length - 2; indexA++) {
const a = nums[indexA];
if (a > 0) return result;
if (a === nums[indexA - 1]) continue;
let indexB = indexA + 1;
let indexC = nums.length - 1;
// Now check if sum is zero, and if NOT, then run the next set of 2 if loop to update indexB and indexC
while (indexB < indexC) {
const b = nums[indexB];
const c = nums[indexC];
if ((a + b + c) === 0) {
result.push([a, b, c]);
}
// Now with the below 2 if functions, I am just implementing how the indexB and indexC will be incremented and decremented with each iteration and gets feeded back to the above while function ( while (indexB < indexC ))
if ((a + b + c) >= 0) {
while (nums[indexC - 1] === c) { indexC--; } // This is equivalent to continue in my previous implementation
indexC--;
}
if((a + b + c ) <= 0) {
while (nums[indexB + 1] === b) { indexB++ } // This is equivalent to continue in my previous implementation
indexB++
}
}
}
return result;
}
// Performance Test - First create a random array with 3000 elements
var arr = Array.from({length: 2000}, () => Math.floor(Math.random() * 3000));
console.time("Solution-1-Brute Force");
threeSum_Brute(arr);
console.timeEnd("Solution-1-Brute Force");
console.log("***************************************************");
console.time("Solution-2-Sub-Obtimal-2-Pointer");
threeSum2 (arr);
console.timeEnd("Solution-2-Sub-Obtimal-2-Pointer");
console.log("***************************************************");
console.time("Solution-3-Optimized-Two-Pointer");
threeSumTwoPointer(arr);
console.timeEnd("Solution-3-Optimized-Two-Pointer");