LeetCode 第 302 场周赛

总结

本场周赛也比较简单,主要考察哈希运用

2341. 数组能形成多少数对

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. 数位和相等数对的最大和

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. 裁剪数字后查询第 K 小的数字

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. 使数组可以被整除的最少删除次数

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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值