-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum-with-multiplicity.h
36 lines (30 loc) · 1 KB
/
3sum-with-multiplicity.h
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
#ifndef THREE_SUM_WITH_MULTIPLICITY_H_
#define THREE_SUM_WITH_MULTIPLICITY_H_
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace solution {
int threeSumMulti(std::vector<int>& nums, int target) {
// 2 ptrs...
// Runtime: 20 ms, faster than 79.25% of C++ online submissions for 3Sum With Multiplicity.
// Memory Usage: 10.5 MB, less than 74.83% of C++ online submissions for 3Sum With Multiplicity.
//
std::unordered_map<int, int> map;
for (auto n : nums) ++map[n];
auto result = 0;
for (auto& p1 : map)
for (auto& p2 : map) {
int i = p1.first, j = p2.first, k = target - i - j;
if (!map.count(k)) continue;
if (i == j && j == k)
result += map[i] * (map[i] - 1) * (map[i] - 2) / 6;
else if (i == j && j != k)
result += map[i] * (map[i] - 1) / 2 * map[k];
else if (i < j && j < k)
result += map[i] * map[j] * map[k];
}
return result % 1000000007;
}
}
#endif // !THREE_SUM_WITH_MULTIPLICITY_H_