-
Notifications
You must be signed in to change notification settings - Fork 0
/
3Sum With Multiplicity.cpp
40 lines (40 loc) · 1.2 KB
/
3Sum With Multiplicity.cpp
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
class Solution {
public:
int threeSumMulti(vector<int>& arr, int target) {
unordered_map<int, long> freqs;
for (auto i : arr) {
freqs[i]++;
}
sort(arr.begin(), arr.end());
int total = 0;
for (int i = 0; i < arr.size() - 2; i++) {
if (i == 0 || i > 0 && arr[i] != arr[i - 1]) {
int j = i + 1, k = arr.size() - 1;
while (j < k) {
if (j > i + 1 && arr[j] == arr[j - 1]) {
j++;
} else if (k < arr.size() - 1 && arr[k] == arr[k + 1]) {
k--;
} else {
if (arr[j] + arr[k] + arr[i] > target) {
k--;
} else if (arr[j] + arr[k] + arr[i] < target) {
j++;
} else {
if (arr[i] != arr[j] && arr[j] != arr[k]) {
total += (freqs[arr[i]] * freqs[arr[j++]] * freqs[arr[k--]]) % 1000000007;
} else if (arr[i] == arr[j] && arr[j] != arr[k]) {
total += ((freqs[arr[i]] * (freqs[arr[j++]] - 1)) * freqs[arr[k--]] / 2) % 1000000007;
} else if (arr[i] != arr[j] && arr[j] == arr[k]) {
total += (freqs[arr[i]] * (freqs[arr[j++]] * (freqs[arr[k--]] - 1)) / 2) % 1000000007;
} else {
total += ((freqs[arr[i]] * (freqs[arr[j++]] - 1) * (freqs[arr[k--]] - 2)) / 6) % 1000000007;
}
}
}
}
}
}
return total;
}
};