当前位置:网站首页>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;
}
};
边栏推荐
- 文件操作详解
- Card link
- Online XML to JSON tool
- 2022.7.24-----leetcode.1184
- Introduction and construction of consul Registration Center
- leetcode-6130:设计数字容器系统
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- Network RTK UAV test [easy to understand]
- process.env
- TGA file format (waveform sound file format)
猜你喜欢
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference

leetcode-155:最小栈

移动web布局方法
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入

Remote—基本原理介绍
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)

文件操作详解

leetcode-6131:不可能得到的最短骰子序列
随机推荐
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
Scan delete folder problems
Brush questions with binary tree (4)
String of sword finger offer question bank summary (II) (C language version)
“链”接无限可能:数字资产链,精彩马上来!
TGA file format (waveform sound file format)
Detailed explanation of document operation
Introduction and construction of consul Registration Center
test
leetcode-6127:优质数对的数目
[advanced drawing of single cell] 07. Display of KEGG enrichment results
Apache MINA框架「建议收藏」
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
leetcode-6126:设计食物评分系统
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
LeetCode通关:哈希表六连,这个还真有点简单
Online random coin tossing positive and negative statistical tool
Step num problem
How to obtain the subordinate / annotation information of KEGG channel