• 第 363 场 LeetCode 周赛题解


    A 计算 K 置位下标对应元素的和

    在这里插入图片描述
    在这里插入图片描述

    模拟

    class Solution {
    public:
        int pop_cnt(int x) {//求x的二进制表示中的1的位数
            int res = 0;
            for (; x; x >>= 1)
                if (x & 1)
                    res++;
            return res;
        }
    
        int sumIndicesWithKSetBits(vector<int> &nums, int k) {
            int res = 0;
            for (int i = 0; i < nums.size(); i++)
                if (pop_cnt(i) == k)
                    res += nums[i];
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    B 让所有学生保持开心的分组方法数

    在这里插入图片描述

    排序+枚举:首先对数组排序,设选出的学生的元素集合为 { x i } \{x_i\} {xi},未选的学生的元素集合为 { y i } \{y_i\} {yi},则有 m a x { x i } < ∣ { x i } ∣ < m i n { y i } max\{ x_i \} < |\{x_i\}| < min\{ y_i \} max{xi}<{xi}<min{yi},所有若存在选择方案,则选出学生一定是数组的一个前缀,且未选的学生是数组剩余的后缀,枚举可能的前后缀划分情况。

    class Solution {
    public:
        int countWays(vector<int> &nums) {
            sort(nums.begin(), nums.end());
            int n = nums.size();
            int res = 0;
            if (nums[n - 1] < n)//后缀为空
                res++;
            if (nums[0] > 0)//前缀为空
                res++;
            for (int i = 0; i < n - 1; i++) {//枚举前缀nums[0,i]
                if (nums[i] < i + 1 && nums[i + 1] > i + 1)
                    res++;
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    C 最大合金数

    在这里插入图片描述
    在这里插入图片描述

    二分:因为所有合金都需要由同一台机器制造,所有枚举各台机器,二分查找能制造的最大合金数

    class Solution {
    public:
        using ll = long long;
    
        int maxNumberOfAlloys(int n, int k, int budget, vector<vector<int>> &composition, vector<int> &stock, vector<int> &cost) {
            int res = 0;
            for (auto &li: composition) {
                int l = 0, r = 3e8;
                while (l < r) {
                    int mid = (l + r + 1) / 2;
                    ll tmp = 0;
                    for (int i = 0; i < n && tmp <= budget; i++) {
                        if (1LL * mid * li[i] - stock[i] > 0)
                            tmp +=(1LL * mid * li[i] - stock[i]) * cost[i];
                    }
                    if (tmp <= budget)
                        l = mid;
                    else
                        r = mid - 1;
                }
                res = max(res, l);
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    D 完全子集的最大元素和

    在这里插入图片描述

    质因数分解+哈希: 一组数字中每对元素的乘积都是一个完全平方数当且仅当数组中每个元素的奇数次质因子的集合相同 (例: 12 = 2 2 × 3 1 12=2^2\times 3^1 12=22×31 75 = 3 1 × 5 2 75=3^1\times 5^2 75=31×52,它们的奇数次质因子的集合都为 { 3 } \{3\} {3}),枚举元素同时更新奇数次质因子的集合对应的元素和。

    class Solution {
    public:
        using ll = long long;
        long long maximumSum(vector<int> &nums) {
            map<vector<int>, ll> s;
            ll res = 0;
            for (int i = 1; i <= nums.size(); i++) {
                int cur = i;
                vector<int> t;//cur的奇数次质因子的集合
                for (int f = 2; f * f <= cur; f++)//质因数分解
                    if (cur % f == 0) {
                        int cnt_f = 0;
                        while (cur % f == 0) {
                            cur /= f;
                            cnt_f++;
                        }
                        if (cnt_f & 1)//f是奇数次质因子
                            t.push_back(f);
                    }
                if (cur != 1) {
                    t.push_back(cur);
                }
                s[t] += nums[i - 1];//更新元素和
            }
            res = 0;
            for (auto &[_, si]: s)
                res = max(res, si);
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

  • 相关阅读:
    AntSK:打造你的本地AI知识库——离线运行详细教程
    MySQL八股文背诵版(续)
    面试:SurfaceView 和 TextureView
    centos7卸载docker
    【每日一题Day353】LC2525根据规则将箱子分类 | 模拟
    虚拟DOM真的是最快的吗?
    学习Golang的接口
    赶紧收藏!!!我直接上瘾!百万人都在学的 Docker
    Python 基础记录 - 第1课
    linux虚拟机主机和子系统网络连接
  • 原文地址:https://blog.csdn.net/weixin_40519680/article/details/132940818