当前位置:网站首页>Leetcode-6127: number of high-quality pairs
Leetcode-6127: number of high-quality pairs
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-6127: The number of high-quality pairs
subject
Topic linking
I'll give you a subscript from 0 Starting array of positive integers nums And a positive integer k .
If the following conditions are met , Then number pair (num1, num2) yes High quality pairs :
- num1 and num2 all In the array nums in .
- num1 OR num2 and num1 AND num2 The median value of the binary representation of is 1 The sum of digits of is greater than or equal to k , among OR It's bit by bit or operation , and AND It's bit by bit And operation .
return Different The number of high-quality pairs .
If a != c perhaps b != d , Think (a, b) and (c, d) Are two different number pairs . for example ,(1, 2) and (2, 1) Different .
Be careful : If num1 At least... Appears in the array once , Then meet num1 == num2 The number of (num1, num2) It can also be a high-quality number pair .
Example 1:
Input :nums = [1,2,3,1], k = 3
Output :5
explain : There are several high-quality pairs as follows :
- (3, 3):(3 AND 3) and (3 OR 3) The binary representation of is equal to (11) . The value is 1 The sum of digits of is equal to 2 + 2 = 4 , Greater than or equal to k = 3 .
- (2, 3) and (3, 2): (2 AND 3) The binary representation of is equal to (10) ,(2 OR 3) The binary representation of is equal to (11) . The value is 1 The sum of digits of is equal to 1 + 2 = 3 .
- (1, 3) and (3, 1): (1 AND 3) The binary representation of is equal to (01) ,(1 OR 3) The binary representation of is equal to (11) . The value is 1 The sum of digits of is equal to 1 + 2 = 3 .
So the number of high-quality pairs is 5 .
Example 2:
Input :nums = [5,1,1], k = 10
Output :0
explain : There are no good number pairs in this array .

Problem solving
Method 1 : Equivalent conversion
about x|y and x&y Medium 1, On the same bit , If there are both 1, So this one 1 Will be counted twice ; If one is for 1 For another 0, So this one 1 Will be counted once .
Therefore, it can be replaced equivalently , And 、 Or the result Binary bit of 1 The number of and , That is, two numbers are binary digits 1 The number of and .
class Solution {
public:
int getBits(int num){
int res=0;
while(num){
res+=num&1;
num>>=1;
}
return res;
}
long long countExcellentPairs(vector<int>& nums, int k) {
// duplicate removal
sort(nums.begin(),nums.end());
nums.resize(unique(nums.begin(),nums.end())-nums.begin());
// nums.erase(unique(nums.begin(),nums.end()),nums.end()); // Either of these can be
int n=nums.size();
vector<int> cnt(64,0);//bits Number ----> Number
for(int i=0;i<n;i++){
int bits=getBits(nums[i]);// Seeking position 1 The number of
cnt[bits]++;
}
long long res=0;
for(int i=1;i<=60;i++){
for(int j=1;j<=60;j++){
if(i+j>=k){
res+=1ll*cnt[i]*cnt[j];
}
}
}
return res;
}
};
边栏推荐
- [advanced mathematics] [4] indefinite integral
- Vivo official website app full model UI adaptation scheme
- Stock software development
- Struct, enum type and union
- String of sword finger offer question bank summary (II) (C language version)
- leetcode-155:最小栈
- JS scope and scope chain
- FanoutExchange交换机代码教程
- 【高等数学】【1】函数、极限、连续
- leetcode-6130:设计数字容器系统
猜你喜欢

FanoutExchange交换机代码教程
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation

leetcode-79:单词搜索
![[paper reading] unpaired image to image translation using cycle consistent advantageous networks](/img/73/69651dd8ecfdddd1cae13a1d223d51.png)
[paper reading] unpaired image to image translation using cycle consistent advantageous networks
![[advanced mathematics] [3] Application of differential mean value theorem and derivative](/img/a9/3b024dbbb201bee4eed6c9f6ce3001.png)
[advanced mathematics] [3] Application of differential mean value theorem and derivative

Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"

leetcode-6129:全 0 子数组的数目

leetcode-6131:不可能得到的最短骰子序列
![[matlab] download originality documents based on oil monkey script and MATLAB](/img/c2/1788b758778ba73dd02fb0d006869e.png)
[matlab] download originality documents based on oil monkey script and MATLAB
![[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger](/img/14/f2b68dbe4e6a9b8d89ed9ff38f5e11.png)
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
随机推荐
【高等数学】【4】不定积分
[advanced mathematics] [1] function, limit, continuity
Arrow parquet
[noi simulation] string matching (suffix automata Sam, Mo team, block)
How to use buffer queue to realize high concurrent order business (glory Collection Edition)
2022.7.24-----leetcode.1184
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
【单细胞高级绘图】07.KEGG富集结果展示
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
Log in to Baidu online disk with cookies (websites use cookies)
wokerman 自定义写入日志文件
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
Brush questions with binary tree (4)
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
103. (cesium chapter) cesium honeycomb diagram (square)
[tensorrt] dynamic batch reasoning
leetcode-6126:设计食物评分系统
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
Remote—基本原理介绍
LeetCode通关:哈希表六连,这个还真有点简单