forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-special-quadruplets.cpp
46 lines (44 loc) · 1.39 KB
/
count-special-quadruplets.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
41
42
43
44
45
46
// Time: O(n^3)
// Space: O(n)
class Solution {
public:
int countQuadruplets(vector<int>& nums) {
int result = 0;
unordered_map<int, int> lookup;
lookup[nums.back()] = 1;
for (int c = size(nums) - 2; c >= 2; --c) {
for (int b = 1; b < c; ++b) {
for (int a = 0; a < b; ++a) {
if (lookup.count(nums[a] + nums[b] + nums[c])) {
result += lookup[nums[a] + nums[b] + nums[c]];
}
}
}
++lookup[nums[c]];
}
return result;
}
};
// Time: O(n^2) ~ O(n^4)
// Space: O(n^2)
class Solution2 {
public:
int countQuadruplets(vector<int>& nums) {
int result = 0;
unordered_map<int, unordered_set<int>> lookup;
for (int d = size(nums) - 1; d >= 3; --d) {
for (int c = 2; c < d; ++c) {
lookup[nums[d] - nums[c]].emplace(c);
}
}
for (int b = 1; b < size(nums) - 2; ++b) {
for (int a = 0; a < b; ++a) {
result += accumulate(cbegin(lookup[nums[a] + nums[b]]), cend(lookup[nums[a] + nums[b]]), 0,
[&b](const auto&total, const auto& x) {
return b < x ? total + 1 : total;
});
}
}
return result;
}
};