当前位置:网站首页>Game 302 of leetcode
Game 302 of leetcode
2022-07-28 01:54:00 【Shirandexiaowo】
summary
This week's match is also relatively simple , Mainly focus on Hash application
2341. How many pairs can an array form
class Solution {
public:
vector<int> numberOfPairs(vector<int>& nums) {
int ans = 0;
map<int, int> mp;
for (auto& t : nums)
mp[t] ++ ;
for (auto& [u, v] : mp)
ans += v / 2;
return {
ans, (int)nums.size() - ans * 2};
}
};
2342. The maximum sum of digits and equal pairs
class Solution {
public:
vector<int> a[100];
int maximumSum(vector<int>& nums) {
int n = nums.size();
for (auto& t : nums)
a[get(t)].push_back(t);
int maxn = -1;
for (int i = 0; i < 100; i ++ )
{
if (a[i].size() >= 2) {
sort(a[i].begin(), a[i].end());
int n = a[i].size();
maxn = max(maxn, a[i][n - 1] + a[i][n - 2]);
}
}
return maxn;
}
int get(int u) {
int ans = 0;
while (u) {
ans += u % 10;
u /= 10;
}
return ans;
}
};
2343. Query the number K Small numbers
class Solution {
public:
vector<int> smallestTrimmedNumbers(vector<string>& nums, vector<vector<int>>& qs) {
int n = nums.size(), m = qs.size();
vector<pair<string, int>> strs(n);
for (int i = 0; i < n; i ++ ) strs[i] = {
nums[i], i};
vector<int> res;
for (auto& q: qs) {
int k = q[0], trim = q[1];
sort(strs.begin(), strs.end(), [&](pair<string, int>& a, pair<string, int>& b) {
for (int i = a.first.size() - trim; i < a.first.size(); i ++ )
if (a.first[i] < b.first[i])
return true;
else if (a.first[i] > b.first[i])
return false;
return a.second < b.second;
});
res.push_back(strs[k - 1].second);
}
return res;
}
};
2344. The minimum number of deletions that make the array divisible
class Solution {
public:
int minOperations(vector<int> &nums, vector<int> &numsDivide) {
int g = 0;
for (int x : numsDivide) g = gcd(g, x);
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); i++) if (g % nums[i] == 0) return i;
return -1;
}
};
边栏推荐
猜你喜欢

Custom type: structure, enumeration, union

路由策略简介

EEG多元模式分析预测慈善捐赠行为

企业运维实践-使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建

Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses

什么是方法,什么是方法论:了解自我精进提升的底层逻辑

"Do you" want to be a test / development programmer? We strive to sprout

了解Shader

周报、月报有多折磨人?万能报表模板建议收藏!(附模板)

腾讯云HiFlow场景连接器
随机推荐
以“数字化渠道”撬动家用电器消费蓝海,经销商在线系统让企业生意更进一步
IIC读写EEFPROM
DeviceXPlorer OPC Server支持哪些设备?本文已列举出来了
MPLS 隧道实验
js 哪些情况不能用 JSON.parse 、JSON.stringify深拷贝及一个更好的深拷贝方法
GBase 8c 注释信息函数
"Do you" want to be a test / development programmer? We strive to sprout
Hcip day 15
Oracle RAC cluster file directory migration
Zhi Huijun, Huawei's "genius youth", has made a new work, building a "customized" smart keyboard from scratch
写给去不图床用户的一封信
Linux系统彻底删除Mysql
Thinking about some things
2022软件测试技能 Robotframework + SeleniumLibrary + Jenkins web关键字驱动自动化实战教程
Dpdk plug-in of VPP
HCIP第十三天笔记
总结:Prometheus存储
面试官:你确定Redis是单线程的进程吗?
文章复现:超分辨率网络FSRCNN
GBase 8c 备份控制函数(四)