第 119 场 LeetCode 双周赛题解

news/2024/5/18 23:39:27 标签: leetcode, 算法, 动态规划, 滑动窗口, 枚举, 最短路

A leetcode.cn/problems/find-common-elements-between-two-arrays" rel="nofollow">找到两个数组中的公共元素

在这里插入图片描述

模拟

class Solution {
public:
    vector<int> findIntersectionValues(vector<int> &nums1, vector<int> &nums2) {
        unordered_set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end());
        vector<int> res(2);
        for (auto x: nums1)
            if (s2.count(x))
                res[0]++;
        for (auto x: nums2)
            if (s1.count(x))
                res[1]++;
        return res;
    }
};


B leetcode.cn/problems/remove-adjacent-almost-equal-characters/" rel="nofollow">消除相邻近似相等字符

在这里插入图片描述

动态规划:设 p [ i ] [ j ] p[i][j] p[i][j] 为将 w o r d [ 0 , i ] word[0,i] word[0,i] 修改为末位为 j j j 的不含相邻近似相等字符串的最少操作数,枚举可能的 w o r d [ i − 1 ] word[i-1] word[i1] 进行状态转移

class Solution {
public:
    int removeAlmostEqualCharacters(string word) {
        int n = word.size();
        int p[n][26];
        for (int j = 0; j < 26; j++)
            p[0][j] = 1;
        p[0][word[0] - 'a'] = 0;
        for (int i = 1; i < n; i++)
            for (int j = 0; j < 26; j++) {
                p[i][j] = INT32_MAX;
                for (int pre = 0; pre < 26; pre++)
                    if (abs(j - pre) > 1)
                        p[i][j] = min(p[i][j], p[i - 1][pre] + (word[i] - 'a' == j ? 0 : 1));
            }
        int res = INT32_MAX;
        for (int j = 0; j < 26; j++)
            res = min(res, p[n - 1][j]);
        return res;
    }
};

C leetcode.cn/problems/length-of-longest-subarray-with-at-most-k-frequency/" rel="nofollow">最多 K 个重复元素的最长子数组

在这里插入图片描述

滑动窗口+哈希:哈希表记录滑动窗口内数的频率,枚举滑动窗口的左边界,尽可能移动滑动窗口的右边界

class Solution {
public:
    int maxSubarrayLength(vector<int> &nums, int k) {
        int n = nums.size();
        int res = 0;
        unordered_map<int, int> f;
        for (int l = 0, r = -1; l < n; f[nums[l++]]--) {
            while (r + 1 < n && f[nums[r + 1]] + 1 <= k)//滑窗左边界固定时,尽可能移动右边界
                f[nums[++r]]++;
            res = max(res, r - l + 1);
        }
        return res;
    }
};

D leetcode.cn/problems/number-of-possible-sets-of-closing-branches/" rel="nofollow">关闭分部的可行集合数目

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

枚举枚举可能的关闭分部集合,然后在当前关闭情况下跑多源最短路算法,然后判断最大的最短路是否不超过 m a x D i s t a n c e maxDistance maxDistance

class Solution {
public:
    inline int get_bit(int mask, int loc) { return mask >> loc & 1; }//返回mask二进制表示的第loc位

    int pop_cnt(int mask) {//返回mask二进制表示中1的个数
        int res = 0;
        for (; mask; mask >>= 1)
            if (mask & 1)
                res++;
        return res;
    }

    int numberOfSets(int n, int maxDistance, vector<vector<int>> &roads) {
        int inf = INT32_MAX;
        vector<vector<int>> g(n, vector<int>(n, inf));
        for (auto &e: roads) {
            g[e[0]][e[1]] = min(g[e[0]][e[1]], e[2]);
            g[e[1]][e[0]] = g[e[0]][e[1]];
        }
        int res = 0;
        for (int mask = 0; mask < (1 << n); mask++) {//枚举关闭分部集合:mask二进制中第i位为0表示第i个分部关闭
            auto t = g;
            for (int k = 0; k < n; k++)
                if (get_bit(mask, k))
                    for (int i = 0; i < n; i++)
                        if (get_bit(mask, i))
                            for (int j = 0; j < n; j++)
                                if (get_bit(mask, j))
                                    if (t[i][k] != inf && t[k][j] != inf)
                                        t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
            int mx = 0;//最大的最短路
            for (int i = 0; i < n; i++)
                if (get_bit(mask, i))
                    for (int j = 0; j < n; j++)
                        if (j != i && get_bit(mask, j))
                            mx = max(mx, t[i][j]);
            if (mx <= maxDistance)
                res++;
        }
        return res;
    }
};

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

相关文章

JAVA+SSM+springboot+MYSQL企业物资库存进销存管理系统

。该系统从两个对象&#xff1a;由管理员和员工来对系统进行设计构建。主要功能包括首页、个人中心、员工管理、项目信息管理、仓库信息管理、供应商管理、项目计划管理、物资库存管理、到货登记管理、物资出库管理、物资入库管理等功能进行管理。本企业物资管理系统方便员工快…

HarmonyOS应用开发-闪屏启动页

这是鸿蒙开发者网站的一个应用《溪村小镇》示例代码&#xff0c;把闪屏启动页单拿出来&#xff0c;分析一下代码。 一、先上效果图 这是应用打开时的一个启动页&#xff0c;启动页会根据三个时间段&#xff08;白天、傍晚、晚上&#xff09;来分别展示溪村小镇不同的景色。 二…

光学遥感显著目标检测初探笔记总结

目录 观看地址介绍什么是显著性目标检测根据不同的输入会有不同的变体(显著性目标检测家族)目前这个领域的挑战 技术方案论文1(2019)论文2(2021)论文3(2022) 未来展望 观看地址 b站链接 介绍 什么是显著性目标检测 一张图片里最吸引注意力的部分就是显著性物体&#xff0c;…

【小米电脑管家】安装使用教程--非小米电脑

安装说明功能体验下载资源 Xiaomi HyperOS发布后&#xff0c;小米妙享电脑端独立版本也走向终点&#xff0c;最新的【小米电脑管家】将会内置妙享实现万物互联。那么本篇文章将分享非小米电脑用户如何绕过设备识别验证安装使用【小米电脑管家】实现万物互联 安装说明 1.解压文…

区块链的可拓展性研究【02】layer2

为什么我们需要二层网络&#xff1f; 区块链的三个目标属性是去中心化、安全和可扩展&#xff0c;区块链三难困境(opens in a new tab)中指出&#xff0c;简单的区块链架构只能实现三个属性中的两个。想要安全的去中心化区块链吗&#xff1f;这意味着你需要牺牲可扩展性。 以太…

游戏开发库

整理了38个Python游戏开发库 https://zhuanlan.zhihu.com/p/505095419 https://zhuanlan.zhihu.com/p/262012936 2023 年最佳游戏引擎推荐 https://zhuanlan.zhihu.com/p/624771157 十大开源游戏引擎深入比较之美 https://blog.51cto.com/u_15273495/2915535 panda3d https:…

定位分析RCU stall问题

使用RCU_CPU_STALL_CPUTIME 在编译内核时打开CONFIG_RCU_CPU_STALL_CPUTIMEy或者在启动参数中增加 rcupdate.rcu_cpu_stall_cputime1, 这样在发生RCU STALL告警时就会有下面附加信息: rcu: hardirqs softirqs csw/systemrcu: number: 624 45 …

我对迁移学习的一点理解(系列2)

文章目录 我对迁移学习的一点理解 我对迁移学习的一点理解 源域和目标域是相对的概念&#xff0c;指的是在迁移学习任务中涉及到的两个不同的数据集或领域。 源域&#xff08;Source Domain&#xff09;通常指的是已经进行过训练和学习的数据集&#xff0c;它被用来提取特征、…