当前位置:网站首页>力扣第 305 场周赛
力扣第 305 场周赛
2022-08-08 02:08:00 【leimingzeOuO】
6136. 算术三元组的数目
class Solution {
public:
int arithmeticTriplets(vector<int>& nums, int diff) {
int n=nums.size();
int res=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(nums[j]-nums[i]==diff&&nums[k]-nums[j]==diff)res++;
}
}
}
return res;
}
};
6139. 受限条件下可到达节点的数目
class Solution {
public:
int reachableNodes(int n, vector<vector<int>>& edges, vector<int>& restricted) {
int res=1;
unordered_map<int,vector<int>>g;
for(auto t:edges)
{
int a=t[0],b=t[1];
g[a].push_back(b);
g[b].push_back(a);
}
unordered_set<int>s={
restricted.begin(),restricted.end()};
queue<int>q;
vector<bool>st(n+10,false);
q.push(0);
st[0]=true;
while(q.size())
{
auto t=q.front();
q.pop();
for(auto x:g[t])
{
if(!st[x]&&!s.count(x))
{
res++;
q.push(x);
st[x]=true;
}
}
}
return res;
}
};
6137. 检查数组是否存在有效划分
class Solution {
public:
bool validPartition(vector<int>& nums) {
int n=nums.size();
vector<bool>f(n+10,false);
f[0]=true;
for(int i=1;i<n;i++)
{
if (f[i - 1] && nums[i] == nums[i - 1] ||
i > 1 && f[i - 2] && (nums[i] == nums[i - 1] && nums[i] == nums[i - 2] ||
nums[i] == nums[i - 1] + 1 && nums[i] == nums[i - 2] + 2))
f[i+1]=true;
}
return f[n];
}
};
6138. 最长理想子序列
class Solution {
public:
int longestIdealString(string s, int k) {
int n=s.size();
vector<int>f(26,0);
for(int i=0;i<s.size();i++)
{
int c=s[i]-'a';
int m=0;
for(int j=max(0,c-k);j<=min(25,c+k);j++)
m=max(m,f[j]);
f[c]=m+1;
}
int res=0;
for(int i=0;i<26;i++)res=max(res,f[i]);
return res;
}
};
边栏推荐
- Overseas Metaverse media must pay attention to the integrity of the propaganda
- 5、基于EasyExcel的导入导出
- After nibbling on this Ali manual, the new year will enter Ali from seven sides
- SQL提问!求帮助,急!
- PTA Exercise 1.8 Binary Search
- 案例分析 | 宜家以双钻设计模型探索线上零售新业务
- 基于图像二维熵的视频信号丢失检测(Signal Loss Detection)
- C#《原CSharp》第四回 人常见岁月更替 却难知人文相继
- SwiftUI使用MatchedGeometryEffect快速同步不同继承层级中视图的位置和尺寸
- 在R中使用Matlab函数
猜你喜欢
随机推荐
期货公司有各种的开户手续费
已知某路口发生事故的频率是平均每天2次,求此处一天内发生4次事故的概率。
typescript assistive technology
Docker install Redis 6.2.6
IDEA 工具类及其余类方法测试方式
2022华数杯数学建模 A题B题C题 思路模型资料汇总
Seven Steps to Hands-On Machine Learning
PTA 习题1.8 二分查找
案例:ELK日志分析系统
Enterprise learning (11) to build the main branch, trigger, nailing notice
pycharm连接远程服务器
Case: ELK log analysis system
puzzle(018.4)珍珑棋局
textplot包:文本语义及可视化
Overseas Metaverse media must pay attention to the integrity of the propaganda
win10 rdkit下载及使用
系统滴答及Systick定时器
陈强教授《机器学习及R应用》课程第十一章作业
网上期货开户越来越普及
【HDLBits 刷题 7】Circuits(3)Sequential Logic---Counters








