• 力扣371周赛


    力扣第371场周赛

    找出强数对的最大异或值 I

    枚举

    class Solution {
    public:
        int maximumStrongPairXor(vector<int>& a) {
            int n = a.size() , res = 0;
            for(int i = 0 ; i < n ; i ++){
                for(int j = 0 ; j < n ; j ++){
                    if(abs(a[i]-a[j])<=min(a[i],a[j])){
                        int c = (a[i]^a[j]);
                        res = max(res , c);
                    }
                }
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    高访问员工

    排序后模拟枚举每个点模拟一下

    class Solution {
    public:
        vector<string> findHighAccessEmployees(vector<vector<string>>& as) {
            unordered_map<string,vector<string>>mp;
            vector<string>ans;
            int n = as.size();
            for(int i = 0 ; i < n ; i ++){
                mp[as[i][0]].push_back(as[i][1]);
            }
            for(auto x: mp){
                string c = x.first;
                vector<string>tmp = x.second;
                int m = tmp.size();
                if(m < 3)continue;
                ranges::sort(tmp);
                for(int i = 1 ; i < m - 1; i ++){
                    int last=stoi(tmp[i-1]);
                    int cur = stoi(tmp[i]);
                    int next=stoi(tmp[i+1]);
                    if(abs(cur - last) < 100 && abs(next - cur) < 100 && abs(next - last) < 100){
                        ans.push_back(c);
                        break;
                    }
                }
            }
            return ans;
        }
    };
    
    • 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

    最大化数组末位元素的最少操作次数

    枚举一下最后一个要不要交换

    class Solution {
    public:
        int check(int a ,int b ,int c1 ,int c2){
            if((a <= c1) && (b<=c2))return 0;
            if(a > c1){
                if(a > c2)return -1;
                if(b > c1)return -1;
                return 1;
            }
            if(b > c2){
                if(b > c1)return -1;
                if(a > c2)return -1;
                return 1;
            }
            return 0;
        }
        int minOperations(vector<int>& nums1, vector<int>& nums2) {
            int ans1 = 0,ans2 = 1, n = nums1.size();
            int c1 = 0 , c2 = 0;
            bool f1 = true , f2 = true;
            c1 = nums1[n-1],c2=nums2[n-1];
            for(int i = n - 2 ; i >= 0 ; i --){
                int s = check(nums1[i],nums2[i],c1,c2);
                if(s == -1){
                    f1 = false;
                    break;
                }
                ans1 += s;
            }
            c1 = nums2[n-1],c2=nums1[n-1];
            for(int i = n - 2 ; i >= 0 ; i --){
                int s = check(nums1[i],nums2[i],c1,c2);
                if(s == -1){
                    f2 = false;
                    break;
                }
                ans2+=s;
            }
            if(f1 && f2){
                return min(ans1,ans2);
            }
            if(f1 == true && f2 == false)return ans1;
            if(f1 == false && f2 == true)return ans2;
            return -1;
        }
    };
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    找出强数对的最大异或值 II

    排序+01trie

    // 代码来自灵神
    class Node {
    public:
        array<Node*, 2> children{};
        int cnt = 0; // 子树大小
    };
    
    class Trie {
        static const int HIGH_BIT = 19;
    public:
        Node *root = new Node();
    
        // 添加 val
        void insert(int val) {
            Node *cur = root;
            for (int i = HIGH_BIT; i >= 0; i--) {
                int bit = (val >> i) & 1;
                if (cur->children[bit] == nullptr) {
                    cur->children[bit] = new Node();
                }
                cur = cur->children[bit];
                cur->cnt++; // 维护子树大小
            }
        }
    
        // 删除 val,但不删除节点
        // 要求 val 必须在 trie 中
        void remove(int val) {
            Node *cur = root;
            for (int i = HIGH_BIT; i >= 0; i--) {
                cur = cur->children[(val >> i) & 1];
                cur->cnt--; // 维护子树大小
            }
        }
    
        // 返回 val 与 trie 中一个元素的最大异或和
        // 要求 trie 不能为空
        int max_xor(int val) {
            Node *cur = root;
            int ans = 0;
            for (int i = HIGH_BIT; i >= 0; i--) {
                int bit = (val >> i) & 1;
                // 如果 cur.children[bit^1].cnt == 0,视作空节点
                if (cur->children[bit ^ 1] && cur->children[bit ^ 1]->cnt) {
                    ans |= 1 << i;
                    bit ^= 1;
                }
                cur = cur->children[bit];
            }
            return ans;
        }
    };
    
    class Solution {
    public:
        int maximumStrongPairXor(vector<int> &nums) {
            sort(nums.begin(), nums.end());
            Trie t{};
            int ans = 0, left = 0;
            for (int y: nums) {
                t.insert(y);
                while (nums[left] * 2 < y) {
                    t.remove(nums[left++]);
                }
                ans = max(ans, t.max_xor(y));
            }
            return ans;
        }
    };
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

  • 相关阅读:
    springboot幼儿园幼儿基本信息管理系统毕业设计源码201126
    有效降低数据库存储成本方案与实践 | 京东云技术团队
    计算机毕业设计 基于SpringBoot的宠物商城网站系统的设计与实现 Java实战项目 附源码+文档+视频讲解
    web安全学习笔记【17】——信息打点(7)
    CentOS 7注册服务开机自启动
    受众分析与卸载分析全面升级,HMS Core分析服务6.6.0版本上新
    【Kafka三】Kakfa API
    LeetCode_容斥原理_中等_223.矩形面积
    4.1_5 文件存储空间管理
    洛谷P3092 状压dp
  • 原文地址:https://blog.csdn.net/qq_60755751/article/details/134359499