力扣382周赛

力扣第382场周赛

按键变更的次数

模拟

class Solution {
public:
    bool che(char a , char b){
        char c;
        if(a >= 'a' && a <= 'z'){
            c = a - 32;
        }else c = a + 32;
        if(a == b || b == c)return true;
        return false;
    }
    int countKeyChanges(string s) {
        int n = s.size();
        int ans = 0;
        for(int i = 1 ; i < n ; i ++){
            if(che(s[i] , s[i - 1]))continue;
            ans ++;
        }
        return ans;
    }
};

子集中元素的最大数量

从小到大模拟枚举,加上特判

class Solution {
public:
    int maximumLength(vector<int>& nums) {
        int n = nums.size();
        map<long long,int>m;
        map<long long,bool>st;
        for(auto x : nums){
            m[x] ++;
        }
        sort(nums.begin() , nums.end());
        long long ans = 1;
        if(m[1]){
            if(m[1] % 2 == 0)ans = max(ans , (long long)m[1] - 1);
            else ans = max(ans , (long long)m[1]);
        }
        for(int i = 0 ; i < n ; i ++){
            long long x = nums[i] , len = 0;
            if(st[x] == true)continue;
            st[x] = true;
            while(x != 1 && m[x] >= 2){
                x *= x;
                st[x] = true;
                len += 2;
            }
            if(m[x] >= 1){
                len += 1;
                ans = max(ans , len);
            }else{
                len -= 1;
                ans = max(ans , len);
            }
        }
        return ans;
    }
};

Alice 和 Bob 玩鲜花游戏

奇偶分类

class Solution {
public:
    long long flowerGame(int n, int m) {
        long long res = 0;
        vector<int>v(3);
        for(int x = 1 ; x <= m ; x ++){
            if(x % 2)v[1] ++;
            else v[2] ++;
        }
        for (int i = 1; i <= n; ++i)
        {
            if (i % 2 == 0)
                res += v[1];
            else
                res += v[2];
        }
        return res;
    }
};

给定操作次数内使剩余元素的或值最小

试填法,看答案的哪一位是否能为1

class Solution {
public:
    int minOrAfterOperations(vector<int> &nums, int k) {
        int ans = 0, mask = 0;
        for (int b = 29; b >= 0; b--) {
            mask |= 1 << b;
            int cnt = 0, and_res = -1; // -1 的二进制全为 1
            for (int x : nums) {
                and_res &= x & mask;
                if (and_res) { //这段还没完
                    cnt++; // 合并 x,操作次数加一
                } else {//这段完了
                    and_res = -1; // 准备合并下一段
                }
            }
            if (cnt > k) {
                ans |= 1 << b; // 答案的这个比特位必须是 1
                mask ^= 1 << b; // 后面不考虑这个比特位
            }
        }
        return ans;
    }
};

相关推荐

  1. 382

    2024-03-10 06:22:04       21 阅读
  2. 381

    2024-03-10 06:22:04       38 阅读
  3. 388(A~B)

    2024-03-10 06:22:04       16 阅读
  4. 375

    2024-03-10 06:22:04       36 阅读
  5. 376

    2024-03-10 06:22:04       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 06:22:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 06:22:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 06:22:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 06:22:04       18 阅读

热门阅读

  1. 人机环境系统与媒体

    2024-03-10 06:22:04       24 阅读
  2. 【AIGC调研系列】大模型的system prompt破解调研

    2024-03-10 06:22:04       20 阅读
  3. Spring MVC 简单文件上传

    2024-03-10 06:22:04       22 阅读
  4. 大模型概念解析 | Prompt Engineering

    2024-03-10 06:22:04       19 阅读
  5. Git基于master创建新分支

    2024-03-10 06:22:04       20 阅读
  6. linux+边缘部署学习记录

    2024-03-10 06:22:04       24 阅读
  7. MongoDB聚合运算符:$dayOfMonth

    2024-03-10 06:22:04       26 阅读
  8. MySQL中的索引

    2024-03-10 06:22:04       24 阅读
  9. selenium链家二手房信息爬取

    2024-03-10 06:22:04       16 阅读