-
Notifications
You must be signed in to change notification settings - Fork 116
/
3Sum With Multiplicity.js
44 lines (43 loc) · 1.08 KB
/
3Sum With Multiplicity.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
// Runtime: 141 ms (Top 80.77%) | Memory: 43.6 MB (Top 86.54%)
/**
* @param {number[]} arr
* @param {number} target
* @return {number}
*/
var threeSumMulti = function(arr, target) {
let count=0
arr.sort((a,b)=>a-b)
for(let i=0;i<arr.length-2;i++){
let j=i+1,k=arr.length-1
while(j<k){
let sum=arr[i]+arr[j]+arr[k]
if(sum<target){
j++
}
else if(sum>target){
k--
}
else{
if(arr[j]!==arr[k]){
let j1=j,k1=k
while(arr[j]===arr[j1]){
j1++
}
while(arr[k]===arr[k1]){
k1--
}
count+=((j1-j)*(k-k1))
j=j1
k=k1
}
else{
for(let n=1;n<=k-j;n++){
count+=n
}
break
}
}
}
}
return count% (10**9 + 7)
};