Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bafd820 commit 5dadae9Copy full SHA for 5dadae9
cpp/923_3_sum_with_multiplicity.cpp
@@ -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