当前位置:网站首页>Game 302 of leetcode
Game 302 of leetcode
2022-07-25 05:20:00 【William_ Tao (siege lion)】
LeetCode The first 302 Weekly match
The first 302 Weekly match
I walk very slowly ! But I never step back !
6120. How many pairs can an array form

Ideas :
If the elements appear in pairs , Then it must be 2 to be divisible by , therefore , We store the number of numbers appearing in the array , Then traverse map, Yes map Divide by elements 2 operation , Can be 2 Divisible is start, Can not be 2 Divisible is the number of remaining elements .
class Solution {
public:
vector<int> numberOfPairs(vector<int>& nums) {
map<int,int> mp;
for(int i=0;i<nums.size();i++){
mp[nums[i]]++;}
int start=0,end=0;
for(auto&[x,y]:mp){
//cout<<y<<"-";
start+=y/2;
end += y%2;
}
return vector<int>{
start,end};
}
};
6164. The maximum sum of digits and equal pairs

Ideas :
1. Find the sum of the digits of each number
2.map Save digit sum and the maximum number under the current digit sum ( greedy )
3. Update answers .
class Solution {
public:
int maximumSum(vector<int>& nums) {
// Like this map Is the key
unordered_map<int,vector<int>>mp;
// Calculate the digit sum of each number and put it in map
for (int x : nums) {
int t = 0;
for (int y = x; y; y /= 10) t += y % 10;
// Corresponding to the number of bits and the array stored under
mp[t].push_back(x);
}
int ans=-1;
for(auto it=mp.begin();it!=mp.end();it++){
// If the length of the array under the current digit sum is greater than one
if(it->second.size()>1){
// Sort , Take the largest two first and add them
sort(it->second.begin(),it->second.end());
int sz=it->second.size();
// Take the largest overall
ans=max(ans,it->second[sz-1]+it->second[sz-2]);
}
}
return ans;
}
};
6121. Query the number K Small numbers

Ideas :
The core of this problem is to bind the trimmed string and the corresponding subscript , And then we do a sort , So you can use pair To store the trimmed string and the corresponding subscript information , Re pass vector Sort .
- First , Traverse queries, obtain k and trim
- Then traverse again based on nums, Get the corresponding truncated string and subscript , Deposit in vector<pair<string,int>> among
- Yes vector<pair<string,int>> Sort
- Last , find nums pass the civil examinations ki Subscript corresponding to small number
class Solution {
public:
vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& queries) {
vector<int> ans(queries.size(),0);
// Record the length of each string
int nSize = nums[0].size();
// Loop traversal query
for(int i=0;i<queries.size();i++){
// Get the k and trim
int k = queries[i][0];
int trim=queries[i][1];
// Number of access intercepts , And its subscript
vector<pair<string,int>> t(nums.size());
// Traverse nums, Access the intercepted string and the corresponding subscript
for(int j=0;j<nums.size();j++){
//string For the intercepted string ,int Is the corresponding subscript
t[j] = make_pair(nums[j].substr(nSize-trim),j);
}
// Yes vector<pair<string,int>> Sort by type , In this way, the subscript will not be lost
// When the element is pair when ,vector The default sort is string Than the size
sort(t.begin(),t.end());
/* for(int j=0;j<nums.size();j++){ cout<<t[j].first<<":"<<t[j].second<<endl; }*/
// look for nums pass the civil examinations ki Subscript corresponding to small number
ans[i] = t[k-1].second;
}
return ans;
}
};
6122. The minimum number of deletions that make the array divisible

Ideas :
Being divisible by a pile of numbers is being divisible by the greatest common divisor of the pile
- First find numsDivide The greatest common factor in
- stay nums( After sorting ) To find out whether there is any conformity numsDivide Elements with equal greatest common factors in ( That is, elements that can be divisible ), If so, return the subscript .( This is the advantage of using sorting , In this way, the minimum number of deletes can be obtained )
class Solution {
public:
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
int g=0;
// Find out numsDivide The largest common factor in
for(int x:numsDivide){
g=gcd(g,x);
}
//cout<<g<<endl;
// Yes nums Sort
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size();i++){
// If in nums Found in and g Equal elements , Returns the current subscript ( That is, the minimum number of deletes )
if(g%nums[i]==0)return i;
}
return -1;
}
};
summary
In general, the difficulty of this week's competition is not big ( Compared with the previous times ), But it's not done well .
There are the following problems in the reflection summary :
1. about stl In the library map,pair This kind is not very commonly used , It's a little strange to operate
2. I've been playing outside recently , The feeling of doing questions seems to be gone ( Make your own dishes , And make excuses )
3.t1 Mainly from other perspectives ( Whether parity can be divisible );t2 simulation + greedy , But through unordered_map<int,vector>mp; To store arrays , Before is map<int,int> such ; Need to learn ;t3 simulation , Use vector<pair<string,int>> To store clipped strings and subscripts , Easy to sort ;t4 Being divisible by a pile of numbers is being divisible by the greatest common divisor of the pile ; Use to gcd Function of
边栏推荐
- unity 3D物体添加 点击事件
- [untitled]
- Build keyword driven automated testing framework
- Permanent magnet synchronous motor 36 question (1) -- what is the difference between salient pole motor and salient pole motor?
- 搭建私有CA服务器
- Redis集群搭建(Windows)
- STL notes (II): template and operator overloading
- Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
- Project management tools - project developer tools
- 自己实现is_class
猜你喜欢
随机推荐
LCP插件创建对等VLAN接口
Add transition layer to JS page
STL notes (II): template and operator overloading
OpenFegin远程调用丢失请求头问题
Compile ue5.0
如何判断是否遭到DDOS攻击
Necessary skills for mobile terminal test: ADB command and packet capturing
Purpose of setting novice task period in the integral system
deep报错
[cloud co creation] design Huawei cloud storage architecture with the youngest cloud service hcie (Part 1)
服务器防护的七个建议
Project management tools - project developer tools
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB
自己实现is_convertible
Zhanrui's first 5g baseband chip was officially released and successfully ranked among the first tier of 5g!
[small program practice] first day
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
[the most comprehensive and detailed] how to design a database and table splitting scheme that can dynamically expand and shrink capacity?
Document collaboration tool recommendation
搭建私有CA服务器







