-
Notifications
You must be signed in to change notification settings - Fork 0
/
countExcellentPairs.cpp
63 lines (58 loc) · 1.89 KB
/
countExcellentPairs.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public:
int get_cnt(int n) {
int ans = 0;
while (n) {
n = n & (n - 1);
++ans;
}
return ans;
}
long long countExcellentPairs(vector<int>& nums, int k) {
map<int, unordered_set<int> > m;
for (int e: nums)
m[get_cnt(e)].insert(e);
vector<int> cnt, prefix_sum{0};
for (auto &it: m) {
cnt.push_back(it.first);
prefix_sum.push_back(it.second.size() + prefix_sum.back());
}
long long ans = 0LL;
int i = 0;
for (; i < cnt.size() && (cnt[i] << 1) < k; ++i) {
auto it = lower_bound(cnt.begin() + 1, cnt.end(), k - cnt[i]);
if (it == cnt.end())
continue;
int j = it - cnt.begin();
ans += (1LL * m[cnt[i]].size() * (prefix_sum.back() - prefix_sum[j])) << 1;
}
if (i < cnt.size()) {
int temp = prefix_sum.back() - prefix_sum[i];
ans += 1LL * temp * temp;
}
return ans;
}
};
class Solution {
static constexpr int U = 30;
public:
long long countExcellentPairs(vector<int> &nums, int k) {
int cnt[U] = {};
for (int x : unordered_set<int>(nums.begin(), nums.end())) // 去重
++cnt[__builtin_popcount(x)];
long ans = 0L;
int s = 0;
for (int i = k; i < U; ++i)
s += cnt[i];
for (int cx = 0; cx < U; ++cx) {
ans += (long) cnt[cx] * s;
int cy = k - 1 - cx;
if (0 <= cy && cy < U) s += cnt[cy];
}
return ans;
}
};
// 作者:endlesscheng
// 链接:https://leetcode.cn/problems/number-of-excellent-pairs/solution/deng-jie-zhuan-huan-pythonjavacgo-by-end-2qzs/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。