There's actually a much simpler algorithm, which is also quadratic, that looks much more like the solution to "2SUM". It requires a lot less 'cleverness', and is more obviously correct:
Find all the solutions with only one distinct element (i.e., [0, 0, 0]). This can be done in linear time (count how many 0s are in the input)
Find all solutions with only two distinct elements (i.e., [x, x, -2*x]). This can be done in linear time, and is basically the same as "2SUM".
Find all solutions with three distinct elements.
First, sort and throw away duplicates (we don't care about duplicates, because they would be captured in steps (1) and (2)
For every (increasing) pair of elements, for (let a = 0; a < nums.length; a++) { for (let b = a + 1; b < nums.length; b++) {
check if the value -(a + b) is in the array (like "2SUM"), and-(a + b) is bigger nums[b]. If so, we found a solution
The algorithm takes time proportional to the square of nums.length, just like your solution. Most programming problems on sites allow a fairly generous constant multiple of the fastest possible solution because they are usually just trying to get you on the right algorithmic solution and don't care so much about fine details.
On this particular problem, it looks like most solutions take around 160ms, but this one takes about 200ms.
// Returns a mapping of// value => [first index `value` appears, last index `value` appears]functionbuildIndex(nums){letindexesByValue={};for(leti=0;i<nums.length;i++){letnum=nums[i];indexesByValue[num]=indexesByValue[num]||[i,i];indexesByValue[num][1]=i;}returnindexesByValue;}functionunique(list){letout=[];for(letnoflist){if(out[out.length-1]!==n){out.push(n);}}returnout;}/**
* @param {number[]} nums
* @return {number[][]}
*/varthreeSum=function(nums){letsolution=[];// Find all solutions with only one distinct element (ie, all zero):if(nums.filter(x=>x===0).length>=3){solution.push([0,0,0]);}// Find all solutions with only two distinct elements (ie, x, and -2x):letindexesByValue=buildIndex(nums);for(letninindexesByValue){if(n==0)continue;if(indexesByValue[n][1]!=indexesByValue[n][0]){if(indexesByValue[-2*n]){solution.push([n,n,-2*n]);}}}// Find all solutions with three distinct elements:nums.sort((a,b)=>a-b);nums=unique(nums);indexesByValue=buildIndex(nums);for(leta=0;a<nums.length;a++){for(letb=a+1;b<nums.length;b++){letdifference=-(nums[a]+nums[b]);if(difference>nums[b]&&indexesByValue[difference]){solution.push([nums[a],nums[b],difference]);}}}returnsolution;};
There's actually a much simpler algorithm, which is also quadratic, that looks much more like the solution to "2SUM". It requires a lot less 'cleverness', and is more obviously correct:
Find all the solutions with only one distinct element (i.e.,
[0, 0, 0]
). This can be done in linear time (count how many0
s are in the input)Find all solutions with only two distinct elements (i.e.,
[x, x, -2*x]
). This can be done in linear time, and is basically the same as "2SUM".Find all solutions with three distinct elements.
for (let a = 0; a < nums.length; a++) { for (let b = a + 1; b < nums.length; b++) {
-(a + b)
is in the array (like "2SUM"), and-(a + b)
is biggernums[b]
. If so, we found a solutionI'd be curious how you'd write that. Seems like it would Timeout, but what do I know?
The algorithm takes time proportional to the square of
nums.length
, just like your solution. Most programming problems on sites allow a fairly generous constant multiple of the fastest possible solution because they are usually just trying to get you on the right algorithmic solution and don't care so much about fine details.On this particular problem, it looks like most solutions take around 160ms, but this one takes about 200ms.
Yeah, definitely runs a bit slower, but you know, lots of ways to open a banana.
I just think "more obviously correct" is a little off base, but you know, whatever works for you.