当前位置:网站首页>Game 302 of leetcode (20220717)
Game 302 of leetcode (20220717)
2022-07-20 05:05:00 【[email protected]】
List of articles
6120. How many pairs can an array form
https://leetcode.cn/problems/maximum-number-of-pairs-in-array/
Ideas :
Use one map Record nums The number of occurrences of each element in the array , And then go through map, If the number of times an element appears >=2, Then the “ logarithm ”(cnt/2) Add to the total logarithm that needs to be removed
class Solution {
public:
vector<int> numberOfPairs(vector<int>& nums) {
unordered_map<int,int> cnt;
for(int num:nums){
cnt[num]++;// Record num Number of occurrences
}
vector<int> ans(2);
int pair=0;
for(auto &kv:cnt){
if(kv.second>=2){
// More than 2
pair+=kv.second/2;
}
}
ans[0]=pair;
ans[1]=nums.size()-2*pair;
return ans;
}
};
6164. The maximum sum of digits and equal pairs
https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/\
Ideas :
Build a map,map In digits and sum As the key ,value It's a list, such as num1 num2 num3 The sum of digits is equal to sum, be list The middle is num1 num2 num3, map When it's done , Traverse map, If map A key corresponds to list There are more than two elements in , Then it's right to list Sort , Take the largest two num
class Solution {
public:
int maximumSum(vector<int>& nums) {
unordered_map<int,vector<int>> map;
int maxsum=-1;// The default value is -1
for(int num:nums){
int sum=get_digit_sum(num);
map[sum].push_back(num);
}
for(auto &kv:map){
if(kv.second.size()>=2){
// A certain digit and two or more corresponding num
vector<int> v=kv.second;
sort(v.begin(),v.end(),greater<int>());// Corresponding multiple num Descending
maxsum=max(maxsum,v[0]+v[1]);// Take the first two largest
}
}
return maxsum;
}
int get_digit_sum(int n){
int sum=0;
while(n>0){
sum+=(n%10);
n/=10;
}
return sum;
}
};
6121. Query the number K Small numbers
https://leetcode.cn/problems/query-kth-smallest-trimmed-number/
Ideas :
First build a subscript array index, The element value is [0,n-1]
, Then on index Array sorting , The sorting rules are based on strings , Specific subscript i And subscripts j The corresponding strings are str[i] and str[j] when , If str[i]==str[j], Then compare i and j, i If smaller ,index[i] be ranked at index[j] front ; Otherwise, according to str[i]<str[j] The regular arrangement of index[i],index[j], If str[i]<str[j] be index[i]<index[j]
class Solution {
public:
vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& queries) {
int n=nums.size();
vector<int> ans;
for(auto &q:queries){
int k=q[0],len=q[1];
vector<string> strs;
for(int i=0;i<n;i++){
strs.push_back(nums[i].substr(nums[i].size()-len));
}
int index[n];
iota(index,index+n,0);// fill index Array value from 0-n-1
sort(index,index+n,[&](int x,int y){
if(strs[x]==strs[y]){
return x<y;// The strings are the same The small subscript is in front
}
return strs[x]<strs[y];
});
ans.push_back(index[k-1]);// The first k Subscript corresponding to small element
}
return ans;
}
};
6122. The minimum number of deletions that make the array divisible
https://leetcode.cn/problems/minimum-deletions-to-make-array-divisible/
Ideas : If nums There is an element num May be numsDivide Divide all elements in , The problem can be translated into num Can be numsDivide Of all elements in The least common divisor to be divisible by , So first find numsDivide Of all elements in The least common divisor , Then in ascending order nums, Start with the smallest number , Until an element x It can be divided by the least common divisor
Elements x If it can be divided numsDivide All elements of , Equivalent to x It's all numsDivide[i] Factor of , It's also equivalent to xxx yes numsDivide The greatest common factor of all elements maxFac Factor of
class Solution {
public:
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
int maxFac=numsDivide[0];
int ans=0;
for(int i=1;i<numsDivide.size();i++){
maxFac=gcd(maxFac,numsDivide[i]);// Calculation numsDivide The least common factor of all elements in
}
sort(nums.begin(),nums.end());// Ascending
for(int i=0;i<nums.size();i++){
if(minFac%nums[i]!=0){
// Can't be divisible Number of deletions +1
ans++;
}else{
// Find one that can be numsDivide Elements divided by all elements in
break;
}
}
return ans==nums.size()?-1:ans;//ans=size It means that it cannot be divisible return -1
}
int gcd(int a,int b){
int c=a%b;
while(c!=0){
a=b;
b=c;
c=a%b;
}
return b;
}
};
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/201/202207182150540323.html
边栏推荐
- 解决Endnote插入参考文献时导致word闪退问题
- BW延期、CJ重心转移,快看如何借由ACG展持续释放IP价值?
- [sword finger offer] sword finger offer 10- ii Frog jumping on steps
- 谱写融入创客教育的美术新篇
- 暑假第一周训练总结
- MongoDB-插入数据insert、insertOne、insertMany、save用法介绍
- 使用字段参数进行报表翻译
- Is the securities account opened by goucai safe? Where can I open a securities account
- Sg90 steering gear drive, with code
- C language power buckle No. 33 search rotation sort array. Three methods
猜你喜欢
[Jianzhi offer] Jianzhi offer 09 Implementing queues with two stacks
Translation of trust region policy optimization
[sword finger offer] sword finger offer 11 Rotate the minimum number of the array
helm概述及基本使用
涛思数据时序数据库 TDengine 正式入驻青云云市场
ThreadPoolTaskExecutor和ThreadPoolExecutor
The pulse of upgrading is spreading the commercialization territory with the recruitment business
What is pricat message?
VIM editor
【ICML2022】GNNRank: 基于有向图神经网络从两两比较中学习全局排序
随机推荐
[Jianzhi offer] Jianzhi offer 09 Implementing queues with two stacks
BW延期、CJ重心转移,快看如何借由ACG展持续释放IP价值?
MongoDB-插入数据insert、insertOne、insertMany、save用法介绍
rust求两数之和
shell脚本
MySQL --- 多表查询 - 笛卡尔积和正确的多表查询、等值连接和不等值连接、内连接和外连接
解析机器人视觉系统的神奇之处
升级的脉脉,正在以招聘业务铺开商业化版图
Servlet Error instantiating servlet class NoSuchMethodException
深度学习中正样本、负样本、简单样本、困难样本的区别 (简单易懂)
小程序_造按钮
Qt编写物联网管理平台44-告警邮件转发
The pulse of upgrading is spreading the commercialization territory with the recruitment business
Stm32dac output voltage, ADC detection voltage, with code, available
Applet_ Make button
Compose a new art chapter integrated into maker Education
Leetcode daily question (1780. check if number is a sum of powers of three)
lua和go混合调用调试记录支持跨平台(通过C和LuaJit进行实现)
PHP安装Swoole支持DTLS协议
DP路径的数目的系列问题