Skip to content

Commit 5dadae9

Browse files
authored
Create 923_3_sum_with_multiplicity.cpp (qiyuangong#30)
* Create 923_3_sum_with_multiplicity.cpp, by @bhanu1131
1 parent bafd820 commit 5dadae9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cpp/923_3_sum_with_multiplicity.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int threeSumMulti(vector<int>& A, int target) {
4+
unordered_map<int, uint64_t> count;
5+
for (const auto& a : A) {
6+
++count[a];
7+
}
8+
uint64_t result = 0;
9+
for (const auto& kvp1 : count) {
10+
for (const auto& kvp2 : count) {
11+
int i = kvp1.first, j = kvp2.first, k = target - i - j;
12+
if (!count.count(k)) {
13+
continue;
14+
}
15+
if (i == j && j == k) {
16+
result += count[i] * (count[i] - 1) * (count[i] - 2) / 6;
17+
} else if (i == j && j != k) {
18+
result += count[i] * (count[i] - 1) / 2 * count[k];
19+
} else if (i < j && j < k) {
20+
result += count[i] * count[j] * count[k];
21+
}
22+
}
23+
}
24+
return result % static_cast<int>(1e9 + 7);
25+
}
26+
};

0 commit comments

Comments
 (0)