5629. 重新格式化电话号码

模拟

注意一些细节,最后位置是否取值。

class Solution {
public:
string reformatNumber(string number) {
string s;
for (auto c: number)
if (c != ' ' && c != '-')
s += c;
string res;
for (int i = 0; i < s.size();) {
if ((int)s.size() - i > 4) res = res + s[i] + s[i + 1] + s[i + 2] + '-', i += 3;
else {
int x = s.size() - i;
if (x == 2) res = res + s[i] + s[i + 1];
else if (x == 3) res = res + s[i] + s[i + 1] + s[i + 2];
else res = res + s[i] + s[i + 1] + '-' + s[i + 2] + s[i + 3];
res += '-';
i += 4;
}
}
res.pop_back();
return res;
}
};

5630. 删除子数组的最大得分

优先队列

class Solution {
public:
deque<int> q;
map<int,int> mp;
int maximumUniqueSubarray(vector<int>& nums) { int num = 0,mn = 0;
for(int i = 0; i < nums.size(); i++){
if(!mp[nums[i]]) mp[nums[i]] = 1;
else {
while(q.size() && nums[q.front()] != nums[i]) mp[nums[q.front()]] = 0,num -= nums[q.front()],q.pop_front();
if(q.size() && nums[q.front()] == nums[i]) num -= nums[q.front()],q.pop_front();
}
q.push_back(i);
num += nums[i];
mn = max(num,mn);
}
return mn;
}
};

1686. 石子游戏 VI

优先队列优化dp

\[\begin{align*}
& dp[i] = max(dp[i-1]~dp[(i-k)] + nums[i]);\\
\end{align*}
\]
class Solution {
public:
//dp[i] = max(dp[i-1]~dp[(i-k)] + nums[i]);
int dp[100000 + 10];
deque<int> q;
int maxResult(vector<int>& nums, int k) {
memset(dp,128,sizeof dp);
int n = nums.size();
dp[0] = nums[0];
q.push_back(0); for(int i = 1; i < n; i ++){
while(i - q.front() > k) q.pop_front();
dp[i] = dp[q.front()] + nums[i];
while(q.size() && dp[q.back()] < dp[i]) q.pop_back();
}
return dp[n-1];
}
};

也可以用multiset优化

class Solution {
public:
int maxResult(vector<int>& nums, int k) {
int n = nums.size();
vector<int> f(n);
multiset<int> S;
f[0] = nums[0];
S.insert(f[0]);
for (int i = 1; i < n; i ++ ) {
if (i - k - 1 >= 0)
S.erase(S.find(f[i - k - 1]));
f[i] = nums[i] + *S.rbegin();
S.insert(f[i]);
}
return f[n - 1];
}
};

5632. 检查边长度限制的路径是否存在

排序 + 并查集

考虑边,对判断和原始边都进行从小到大排序,对满足限制边的原始边进行建图连边,判断是否存在/满足条件的结果。

排序后可以保证小的边限制能满足的情况下,大边同样能满足。

用并查集判断是否存在相应的边即可。

const int MAXN = 1e5 + 50;

int m, Q;
struct Node{ int u, v, w, i; } edge[MAXN], query[MAXN];
bool ans[MAXN];
bool cmp(const Node &a, const Node &b){ return a.w < b.w; } int father[MAXN];
int getFather(int x){ return father[x] = (father[x] == x ? x: getFather(father[x])); }
void mergeFather(int x, int y){
int fx = getFather(x), fy = getFather(y);
if (fx == fy) return;
if (fx > fy) swap(fx, fy);
father[fx] = fy;
} class Solution {
public:
vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {
m = edgeList.size(); Q = queries.size(); for (int i = 0; i < m; i++){
edge[i].u = edgeList[i][0];
edge[i].v = edgeList[i][1];
edge[i].w = edgeList[i][2];
}
for (int i = 0; i < Q; i++){
query[i].u = queries[i][0];
query[i].v = queries[i][1];
query[i].w = queries[i][2];
query[i].i = i;
} sort(edge, edge + m, cmp); sort(query, query + Q, cmp); for (int i = 0; i <= n; i++) father[i] = i;
for (int i = 0, k = 0; i < Q; i++){
while(k < m && edge[k].w < query[i].w) {
mergeFather(edge[k].u, edge[k].v);
k++;
}
ans[query[i].i] = (getFather(query[i].u) == getFather(query[i].v));
} vector<bool> ret;
for (int i = 0; i < Q; i++) ret.push_back(ans[i]);
return ret;
}
};

Leetcode 220 周赛 题解的更多相关文章

  1. LeetCode #188场周赛题解

    A题链接 给你一个目标数组 target 和一个整数 n.每次迭代,需要从 list = {1,2,3..., n} 中依序读取一个数字. 请使用下述操作来构建目标数组 target : Push:从 ...

  2. LeetCode第29场双周赛题解

    第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...

  3. LeetCode双周赛#33 题解

    5480. 可以到达所有点的最少点数目 #贪心 题目链接 题意 给定有向无环图,编号从0到n-1,一个边集数组edges(表示从某个顶点到另一顶点的有向边),现要找到最小的顶点集合,使得从这些点出发, ...

  4. Leetcode 双周赛#32 题解

    1540 K次操作转变字符串 #计数 题目链接 题意 给定两字符串\(s\)和\(t\),要求你在\(k\)次操作以内将字符串\(s\)转变为\(t\),其中第\(i\)次操作时,可选择如下操作: 选 ...

  5. ACM团队周赛题解(1)

    这次周赛题目拉了CF315和CF349两套题. 因为我代码模板较长,便只放出关键代码部分 #define ll long long #define MMT(s,a) memset(s, a, size ...

  6. 「LeetCode」全部题解

    花了将近 20 多天的业余时间,把 LeetCode 上面的题目做完了,毕竟还是针对面试的题目,代码量都不是特别大,难度和 OJ 上面也差了一大截. 关于二叉树和链表方面考察变成基本功的题目特别多,其 ...

  7. C#版 - Leetcode 65. 有效数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  8. [LeetCode] Three Sum题解

    Three Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? ...

  9. Leetcode的SQL题解:185. 部门工资前三高的员工

    题目 查询部门工资前三高的员工. 我用的数据库是oracle. 下面是数据表的信息. Employee表数据: | ID | NAME | Salary | DepartmentId | | -- | ...

随机推荐

  1. JPA使用之@Query的常用写法

    准备 实体 @Data @Table(name = "task_apply") @Entity public class TaskApply { @Id @GeneratedVal ...

  2. 802.11抓包软件对比之Microsoft Network Monitor

    从事WiFi嵌入式软件开发的同学,802.11协议层抓包分析是一个需要熟练掌握的一个技能,需要通过分析WiFi底层802.11协议层的数据包来定位问题.同时从学习802.11协议的角度而言,最有效的学 ...

  3. GAN和GAN的改进

    GAN 原始GAN中判别器要最小化如下损失函数,尽可能把真实样本分为正例,生成样本分为负例: 其中是真实样本分布,是由生成器产生的样本分布. 第一个式子我们不看梯度符号的话即为判别器的损失函数,log ...

  4. CentOS初级扫盲

    发行版介绍 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX(可移植操作系统接口Portable Operating System Interface ,缩写为 POSIX ...

  5. Django----Serializer序列化

    serializer的两大特征 1.校检数据 2.序列化 首先创建apps/Serializer.py 在序列化里面导包 from rest_framework import serializers ...

  6. no Qt platform plugin could be initialized问题的解决办法

    ☞ ░ 前往老猿Python博文目录 ░ 今天因要使用到一个以前PyQT写得工具,但运行时报错: This application failed to start because no Qt plat ...

  7. Python函数独立星号(*)分隔的命名关键字参数

    如果需要限制关键字参数的输入名字,就需要使用到命名关键字参数的形式,所谓命名关键字参数就是给关键字参数限定指定的名字,输入其他名字不能识别.命名关键字参数和位置参数之间使用独立的星号(*)分隔,星号后 ...

  8. windows下多Python环境指定pip安装模块到对应Python环境下

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿在windows下装了2套Python,一套是直接安装的Pytho ...

  9. PyQt开发样例: 利用QToolBox开发的桌面工具箱Demo

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 toolBox工具箱是一个容器部件,对应类为QToolBox,在其内有一列从上到下顺序排列 ...

  10. PyQt(Python+Qt)学习随笔:QTableWidget的takeItem和sortItems方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget中的takeItem方法从表格中取并去除项,sortItems方法对表格中的 ...