第 371 场 LeetCode 周赛题解

news/2024/5/20 8:26:19 标签: leetcode, 哈希算法, 算法, 滑动窗口, 字典树

A leetcode.cn/problems/maximum-strong-pair-xor-i">找出强数对的最大异或值 I

在这里插入图片描述

模拟

class Solution {
public:
    int maximumStrongPairXor(vector<int> &nums) {
        int n = nums.size();
        int res = 0;
        for (auto x: nums)
            for (auto y: nums)
                if (abs(x - y) <= min(x, y))
                    res = max(res, x ^ y);
        return res;
    }
};

B leetcode.cn/problems/high-access-employees">高访问员工

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

哈希+排序:用哈希表记录各个员工所有的访问时间,并对访问时间排序,然后遍历排序后的相邻三元组判断

class Solution {
public:
    vector<string> findHighAccessEmployees(vector<vector<string>> &access_times) {
        unordered_map<string, vector<int>> li;
        for (auto &it: access_times)//时间转化为总秒数
            li[it[0]].push_back(getint(it[1][0]) * 600 + getint(it[1][1]) * 60 + getint(it[1][2]) * 10 + getint(it[1][3]));
        vector<string> res;
        for (auto &[k, v]: li) {
            sort(v.begin(), v.end());
            for (int i = 2; i < v.size(); i++)
                if (v[i] - v[i - 2] < 60) {
                    res.push_back(k);
                    break;
                }
        }
        return res;
    }

    inline int getint(char ch) {
        return ch - '0';
    }
};

C leetcode.cn/problems/minimum-operations-to-maximize-last-elements-in-arrays">最大化数组末位元素的最少操作次数

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

模拟:分两种情况:1)不交换 n u m s 1 [ n − 1 ] nums1[n - 1] nums1[n1] n u m s 2 [ n − 1 ] nums2[n - 1] nums2[n1] ;2)交换 n u m s 1 [ n − 1 ] nums1[n - 1] nums1[n1] n u m s 2 [ n − 1 ] nums2[n - 1] nums2[n1]。每种情况下遍历 i ∈ [ 0 , n ) i\in [0,n) i[0,n) ,若 n u m s 1 [ i ] > n u m s 1 [ n − 1 ] nums1[i] > nums1[n-1] nums1[i]>nums1[n1] n u m s 2 [ i ] > n u m s 2 [ n − 1 ] nums2[i] > nums2[n-1] nums2[i]>nums2[n1],则交换 n u m s 1 [ i ] nums1[i] nums1[i] n u m s 2 [ i ] nums2[i] nums2[i],若交换后仍有 n u m s 1 [ i ] > n u m s 1 [ n − 1 ] nums1[i] > nums1[n-1] nums1[i]>nums1[n1] n u m s 2 [ i ] > n u m s 2 [ n − 1 ] nums2[i] > nums2[n-1] nums2[i]>nums2[n1],则当前情况无解。

class Solution {
public:
    int inf = 1e7;

    int get(vector<int> nums1, vector<int> nums2) {
        int res = 0;
        for (int i = 0; i < nums1.size(); i++)
            if (nums1[i] > nums1.back() || nums2[i] > nums2.back()) {
                swap(nums1[i], nums2[i]);
                res++;
                if (nums1[i] > nums1.back() || nums2[i] > nums2.back())
                    return inf;
            }
        return res;
    }

    int minOperations(vector<int> &nums1, vector<int> &nums2) {
        int res = get(nums1, nums2);//不交换 nums1[n - 1] 和 nums2[n - 1]的情况
        swap(nums1.back(), nums2.back());
        res = min(res, 1 + get(nums1, nums2));//交换 nums1[n - 1] 和 nums2[n - 1]的情况
        return res == inf ? -1 : res;
    }
};

D leetcode.cn/problems/maximum-strong-pair-xor-ii">找出强数对的最大异或值 II

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

滑动窗口 + 字典树:不妨设 x ≤ y x\le y xy ,则满足 2 × x ≤ y 2\times x\le y 2×xy x x x y y y 可构成强数对,对数组排序后枚举 y y y,同时
滑动窗口维护满足条件的 x x x ,过程中用字典树维护当前满足条件的 x x x 的集合,同时通过字典树查询与 y y y 构成的强数对的最大异或值

class Trie {//字典树
public:
    vector<Trie *> next = vector<Trie *>(2, nullptr);
    int cnt = 0;//子树中的数的数目

    void insert(int v) {//往字典树中插入v
        Trie *cur = this;
        for (int i = 19; i >= 0; i--) {
            cur->cnt++;//更新计数
            int t = v >> i & 1;
            if (!cur->next[t])
                cur->next[t] = new Trie();
            cur = cur->next[t];
        }
        cur->cnt++;//更新计数
    }

    void del(int v) {//删除字典树中的v
        Trie *cur = this;
        for (int i = 19; i >= 0; i--) {
            cur->cnt--;//更新计数
            int t = v >> i & 1;
            cur = cur->next[t];
        }
        cur->cnt--;//更新计数
    }

    int query(int y) {//查询与y构成的强数对的最大异或值
        int res = 0;
        Trie *cur = this;
        for (int i = 19; i >= 0; i--) {
            int t = y >> i & 1;
            if (!cur->next[t ^ 1] || cur->next[t ^ 1]->cnt == 0)
                cur = cur->next[t];
            else {
                cur = cur->next[t ^ 1];
                res |= 1 << i;
            }
        }
        return res;
    }
};

class Solution {
public:
    int maximumStrongPairXor(vector<int> &nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        Trie trie;
        int res = 0;
        for (int iy = 0, ix = 0; iy < n; iy++) {//枚举y (nums[iy])
            trie.insert(nums[iy]);//插入y
            while (2 * nums[ix] < nums[iy])//更新滑动窗口左端点
                trie.del(nums[ix++]);//删除无法与y构成强数对的x
            res = max(res, trie.query(nums[iy]));//查询与y构成的强数对的最大异或值
        }
        return res;
    }
};


http://www.niftyadmin.cn/n/5175559.html

相关文章

迅为龙芯2K1000开发板虚拟机ubuntu启动root用户

作为嵌入式开发人员&#xff0c;系统的所有权限都要为我们打开&#xff0c;所以我们不必像运维那样&#xff0c;对 root 用户非常敏感&#xff0c;所以安装完 ubuntu 系统以后&#xff0c;我们要启用 root 用户。 首先我们打开 ubuntu 控制终端&#xff0c;然后在终端里面输入…

商品管理分页查询实现

<template><el-card><el-row :gutter="20" class="header"><el-col :span="7"><el-input placeholder="请输入商品名称..." clearable v-model="queryForm.query"></el-input></el-…

鸿蒙 API9 接入 Crypto库

鸿蒙 API9 接入 Crypto库 开发环境 API9。 参考文档 之前研究了半天鸿蒙自身支持的算法库&#xff0c;只能说集成起来还是比较麻烦的&#xff0c;不如开箱即用的npm crypto好用。不过之前也没想到三方库会这么快的适配鸿蒙&#xff0c;毕竟小程序都多少年了&#xff0c;各种…

sqlite expert数据库导入编辑好的表格

一、前言 此功能不常用&#xff0c;但是又非常重要&#xff0c;每次想要用忘记了方法还得上网搜索&#xff0c;这里自己记录一下&#xff0c;方便以后查看&#xff0c;也帮助大家快速使用 二、环境 window sqlite3 三、正文 步骤一&#xff1a;在数据库创建空表格&#x…

如何获取1688商品详情,价格,图片

1688是阿里巴巴旗下的B2B电子商务平台&#xff0c;主要面向国内的生产商和批发商。 通过获取到的跨境属性数据&#xff0c;可以了解到商品的跨境属性&#xff0c;例如商品的语言、原产地、适用场景等信息。这些数据可以帮助用户更好地了解商品的特点和质量&#xff0c;做出更明…

Perl爬虫程序的框架

Perl爬虫程序的框架&#xff0c;这个框架可以用来爬取任何网页的内容。 perl #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTML::TreeBuilder; # 创建LWP::UserAgent对象 my $ua LWP::UserAgent->new; # 设置代理信息 $ua->proxy(http, ); …

MATLAB中ginput函数用法

目录 语法 说明 示例 标识点和绘制坐标 返回用于选择坐标的按键 标识地理坐标区上的点 ginput函数的功能是标识坐标区坐标。 语法 [x,y] ginput(n) [x,y] ginput [x,y,button] ginput(___) 说明 [x,y] ginput(n) 可用于标识笛卡尔坐标区、极坐标区或地理坐标区内…

抢抓泛娱乐社交出海新风口!Flat Ads深圳沙龙活动引爆海外市场

随着全球化进程的加速&#xff0c;中国的应用类APP不断走向国际市场。作为产品和服务的提供者&#xff0c;中国开发者围绕社交泛娱乐创新&#xff0c;开启直播出海、短视频出海、游戏社交出海、1V1 视频出海、音频社交出海等出海热潮。“社交、泛娱乐”融合成为行业主流发展趋势…