Leetcode 220 周赛 题解
模拟
注意一些细节,最后位置是否取值。
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;
}
};
优先队列
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;
}
};
优先队列优化dp
& 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 周赛 题解的更多相关文章
- LeetCode #188场周赛题解
A题链接 给你一个目标数组 target 和一个整数 n.每次迭代,需要从 list = {1,2,3..., n} 中依序读取一个数字. 请使用下述操作来构建目标数组 target : Push:从 ...
- LeetCode第29场双周赛题解
第一题 用一个新数组newSalary保存去掉最低和最高工资的工资列表,然后遍历newSalary,计算总和,除以元素个数,就得到了平均值. class Solution { public: doub ...
- LeetCode双周赛#33 题解
5480. 可以到达所有点的最少点数目 #贪心 题目链接 题意 给定有向无环图,编号从0到n-1,一个边集数组edges(表示从某个顶点到另一顶点的有向边),现要找到最小的顶点集合,使得从这些点出发, ...
- Leetcode 双周赛#32 题解
1540 K次操作转变字符串 #计数 题目链接 题意 给定两字符串\(s\)和\(t\),要求你在\(k\)次操作以内将字符串\(s\)转变为\(t\),其中第\(i\)次操作时,可选择如下操作: 选 ...
- ACM团队周赛题解(1)
这次周赛题目拉了CF315和CF349两套题. 因为我代码模板较长,便只放出关键代码部分 #define ll long long #define MMT(s,a) memset(s, a, size ...
- 「LeetCode」全部题解
花了将近 20 多天的业余时间,把 LeetCode 上面的题目做完了,毕竟还是针对面试的题目,代码量都不是特别大,难度和 OJ 上面也差了一大截. 关于二叉树和链表方面考察变成基本功的题目特别多,其 ...
- C#版 - Leetcode 65. 有效数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- [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? ...
- Leetcode的SQL题解:185. 部门工资前三高的员工
题目 查询部门工资前三高的员工. 我用的数据库是oracle. 下面是数据表的信息. Employee表数据: | ID | NAME | Salary | DepartmentId | | -- | ...
随机推荐
- H3CNE(教程)
培训机构提供的ppt,可能也是来自于官方提供,涉及到H3CNE认证考试中的全部知识点,学真技术还得看这个.包括帧中继,哪怕是淘汰了十多年了. https://huxiaoyao.lanzous.com ...
- MySQL下载及使用
MySQL下载及使用 在下载MySQL的过程当中一般都不会下载最新版本的软件,因为最新版本的MySQL可能会出现各种问题,也不推荐在原来的版本上更新到最新版本,因为这样可能导致原本项目能正常运行,更新 ...
- Django 的F查询与Q查询,事物
F查询 Django 提供 F() 来做这样的比较.F() 的实例可以在查询中引用字段,来比较同一个 model 实例中两个不同字段的值 示例1: 查询出卖出数大于库存数的商品 from ...
- shiro利用过期时间,解决用户冻结踢出问题
背景 shiro中需要冻结某个用户,但是此时此刻这个用户在线,如果冻结只是改变状态的话,只会导致用户不满,所以要改变这个办法. 在查找过程中发现都是告诉shiro写自定义过滤器,那么我如果自定义过滤器 ...
- MinIO
MinIO 是一个非常轻量的基于 Apache License v2.0 开源协议的对象存储服务.它兼容亚马逊 S3 云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片.视频.日志文件.备份 ...
- django 不使用序列化器时进行查询结果序列化
在app01views中添加 class User1(View): def post(self,request): user=User.objects.all() list=[] for i in u ...
- node-sass版本问题
node-sass sass-loader的问题 出现了版本的问题 版本太高 版本不兼容解决方法: cnpm i node-sass@4.14.1 cnpm i sass-loader@7.3.1 - ...
- moviepy音视频开发:使用credits1给视频加片头片尾字幕
☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:视频基类VideoClip子类DataVideoClip.UpdatedVideoClip.ImageClip. ...
- 第8.34节 《Python类中常用的特殊变量和方法》总结
本章介绍了Python类中常用的特殊变量和方法,这些特殊变量和方法都有特殊的用途,是Python强大功能的基石之一,许多功能非常有Python特色.由于Python中一切皆对象,理解这些特殊变量和方法 ...
- BUUOJ WEB(1)
[ACTF2020 新生赛]Include 开启环境之后点击tips 可以在url中看到格式为: ?file=flag.php 加上题目是include,可以猜测是文件包含漏洞 http://a291 ...