第 113 场 LeetCode 双周赛题解

news/2024/5/19 1:44:55 标签: leetcode, 算法, 哈希, dfs, 动态规划, 枚举

A leetcode.cn/problems/minimum-right-shifts-to-sort-the-array/">使数组成为递增数组的最少右移次数

在这里插入图片描述

数据范围小直接模拟…

class Solution {
public:
    int minimumRightShifts(vector<int> &nums) {
        for (int op = 0; op < nums.size(); op++) {
            if (is_sorted(nums.begin(), nums.end()))//nums是否已经有序
                return op;
            rotate(nums.begin(), prev(nums.end()), nums.end());//右移一次
        }
        return -1;
    }
};

B leetcode.cn/problems/minimum-array-length-after-pair-removals/">删除数对后的最小数组长度

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

分类讨论:设数组中出现次数最多的数的出现次数为 m x mx mx

  1. 当数组长度为偶数时:若 m x ≤ n 2 mx \le \frac n 2 mx2n 则删除数对后的最小数组长度为 0 0 0 ,否则删除数对后的最小数组长度为 m x − ( n − m x ) mx - (n - mx) mx(nmx)
  2. 当数组长度为偶数时:若 m x ≤ ⌊ n 2 ⌋ mx \le \left \lfloor \frac n 2 \right \rfloor mx2n 则删除数对后的最小数组长度为 1 1 1 ,否则删除数对后的最小数组长度为 m x − ( n − m x ) mx - (n - mx) mx(nmx)
class Solution {
public:
    int minLengthAfterRemovals(vector<int> &nums) {
        unordered_map<int, int> cnt;//统计出现次数
        for (auto x: nums)
            cnt[x]++;
        int mx = 0;
        for (auto &[_, cnt_i]: cnt)
            mx = max(mx, cnt_i);
        int n = nums.size();
        if (n % 2 == 0) 
            return mx <= n / 2 ? 0 : mx - (n - mx);        
        return mx <= n / 2 ? 1 : mx - (n - mx);
    }
};

C leetcode.cn/problems/count-pairs-of-points-with-distance-k/">统计距离为 k 的点对

计数+枚举枚举数组的坐标 ( p [ 0 ] , p [ 1 ] ) (p[0],p[1]) (p[0],p[1])枚举可能的与当前坐标距离为 k k k 的坐标:枚举 0 ≤ i ≤ k 0 \le i \le k 0ik ( p [ 0 ] ∧ i , p [ 1 ] ∧ ( k − i ) ) (p[0]\wedge i, p[1]\wedge (k-i)) (p[0]i,p[1](ki)) 即为与当前坐标距离为 k k k 的坐标,若之前出现过这个坐标则更新答案,在枚举坐标 ( x , y ) (x,y) (x,y) 的过程中记录坐标的出现次数。

class Solution {
public:
    int countPairs(vector<vector<int>> &coordinates, int k) {
        map<pair<int, int>, int> cnt;//记录坐标的出现次数
        int res = 0;
        for (auto &p: coordinates) {
            for (int i = 0; i <= k; i++) {
                int x = p[0] ^ i;
                int y = p[1] ^ (k - i);
                auto tmp = make_pair(x, y);
                auto it = cnt.find(tmp);
                if (it != cnt.end())//之前出现过坐标(x,y)
                    res += it->second;
            }
            cnt[{p[0], p[1]}]++;
        }
        return res;
    }
};

D leetcode.cn/problems/minimum-edge-reversals-so-every-node-is-reachable/">可以到达每一个节点的最少边反转次数

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

动态规划:首先建无向图,同时记录原始边的方向。不妨设 0 0 0 为树根,设 p [ c u r ] p[cur] p[cur] 为:使 c u r cur cur 能够到达以 c u r cur cur 为根的子树中的所有节点的最少边反转次数。通过 d f s dfs dfs 可以求出 p p p 数组。然后从 0 0 0 开始遍历树中节点 c u r cur cur,遍历过程中维护使 c u r cur cur 能够到达除以 c u r cur cur 为根的子树外的所有节点的最少边反转次数 u p _ c o s t up\_cost up_cost,则使 c u r cur cur 能够到达所有节点的最少边反转次数有 r e s [ c u r ] = p [ c u r ] + u p _ c o s t res[cur]=p[cur]+up\_cost res[cur]=p[cur]+up_cost

class Solution {
public:
    vector<int> minEdgeReversals(int n, vector<vector<int>> &edges) {
        vector<pair<int, int>> e[n];
        for (auto &ei: edges) {
            e[ei[0]].emplace_back(ei[1], 0);//0代表正向边
            e[ei[1]].emplace_back(ei[0], 1);//1代表反向边
        }
        int p[n];
        function<int(int, int)> dfs = [&](int cur, int par) {
            p[cur] = 0;
            for (auto &[j, w]: e[cur])
                if (j != par) {
                    if (w == 1)//(cur,j)这条边需要反转
                        p[cur]++;
                    p[cur] += dfs(j, cur);
                }
            return p[cur];
        };
        dfs(0, -1);//求p数组
        vector<int> res(n);
        function<void(int, int, int)> get = [&](int cur, int par, int up_cost) {
            res[cur] = p[cur] + up_cost;
            for (auto &[j, w]: e[cur])
                if (j != par) {
                    if (w == 0)
                        get(j, cur, res[cur] - p[j] + 1);
                    else
                        get(j, cur, res[cur] - p[j] - 1);
                }
        };
        get(0, -1, 0);//求答案数组
        return res;
    }
};



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

相关文章

Python获取当前日期并格式化

使用Python语言获取当前日期并进行格式化非常简便。Python中的datetime模块提供了处理日期和时间的功能&#xff0c;以下是获取当前日期并格式化的示例代码&#xff1a; from datetime import datetime # 获取当前日期 current_date datetime.now() # 格式化日期为 yyyy-MM…

Android 13.0 屏蔽Launcher3桌面app图标的长按功能

1.概述 在13.0的产品定制化开发中,系统默认的Launcher3在workspace 第二屏通常都会显示app列表 点击进入app 列表页,长按app的icon图标会弹出 应用信息 弹窗 等信息,而产品的开发需要,不需要弹出这些信息,所以要求去掉app的icon图标的长按功能 2.屏蔽Launcher3桌面app图…

基于SpringbootShiro实现的CAS单点登录

概述 单点登录&#xff08;Single Sign On,SSO&#xff09;是一种登录管理机制&#xff0c;主要用于多系统集成&#xff0c;即在多个系统中&#xff0c;用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个&#xff0c;无须多次登录。常见的例子就是&#xff0c;…

LeetCode 之 有序数组的平方

算法模拟&#xff1a; Algorithm Visualizer 在线工具&#xff1a; C 在线工具 如果习惯性使用Visual Studio Code进行编译运行&#xff0c;需要C11特性的支持&#xff0c;可参考博客&#xff1a; VisualStudio Code 支持C11插件配置 有序数组的平方 LeetCode 有序数组的平方…

系统架构设计高级技能 · 构件与中间件技术

点击进入系列文章目录 现在的一切都是为将来的梦想编织翅膀&#xff0c;让梦想在现实中展翅高飞。 Now everything is for the future of dream weaving wings, let the dream fly in reality. 系统架构设计高级技能 构件与中间件技术 一、构件的定义二、构件系统架构特性三…

浅谈C++|类的继承篇

引子&#xff1a; 继承是面向对象三大特性之一、有些类与类之间存在特殊的关系&#xff0c;例如下图中: 我们发现&#xff0c;定义这些类时&#xff0c;下级别的成员除了拥有上一级的共性&#xff0c;还有自己的特性。 这个时候我们就可以考虑利用继承的技术&#xff0c;减少…

openGauss学习笔记-71 openGauss 数据库管理-创建和管理普通表-删除表中数据

文章目录 openGauss学习笔记-71 openGauss 数据库管理-创建和管理普通表-删除表中数据 openGauss学习笔记-71 openGauss 数据库管理-创建和管理普通表-删除表中数据 在使用表的过程中&#xff0c;可能会需要删除已过期的数据&#xff0c;删除数据必须从表中整行的删除。 SQL不…

【刷题篇】贪心算法(二)

文章目录 找出工作所需最短时间活动选择无重叠区间 找出工作所需最短时间 某工厂有n个独立的作业&#xff0c;由m台相同的机器进行加工处理。作业i所需的加工时间为ti&#xff0c;任何作业在被处理时不能中断&#xff0c;也不能进行拆分处理。现厂长请你给他写一个程序:算出n个…