当前位置:网站首页>Leetcode 1218. 最长定差子序列
Leetcode 1218. 最长定差子序列
2022-06-24 12:33:00 【我不是萧海哇~~~~】
给你一个整数数组 arr 和一个整数 difference,请你找出并返回 arr 中最长等差子序列的长度,该子序列中相邻元素之间的差等于 difference 。
子序列 是指在不改变其余元素顺序的情况下,通过删除一些元素或不删除任何元素而从 arr 派生出来的序列。
示例 1:
输入:arr = [1,2,3,4], difference = 1
输出:4
解释:最长的等差子序列是 [1,2,3,4]。
示例 2:
输入:arr = [1,3,5,7], difference = 1
输出:1
解释:最长的等差子序列是任意单个元素。
示例 3:
输入:arr = [1,5,7,8,5,3,4,2,1], difference = -2
输出:4
解释:最长的等差子序列是 [7,5,3,1]。
提示:
- 1 <= arr.length <= 10^5
- -104 <= arr[i], difference <= 10^4
Code:
class Solution {
public:
int longestSubsequence(vector<int>& arr, int difference) {
int maxlen=1;
map<int,int>mymap;
pair<map<int, int>::iterator, bool> ret;
map<int,int>::iterator it;
for(int i=0;i<arr.size();i++)
{
int start=arr[i];
if((it=mymap.find(arr[i]-difference))!=mymap.end())
{
continue;
}
ret=mymap.insert(pair<int,int>(arr[i],0));
if(!ret.second)
continue;
int templen=1;
for(int j=i+1;j<arr.size();j++)
{
if((start+difference)==arr[j])
{
templen++;
start+=difference;
}
}
maxlen=max(templen,maxlen);
}
cout<<maxlen<<endl;
return maxlen;
}
};
边栏推荐
- Install MySQL in docker and modify my CNF profile
- Cryptography series: collision defense and collision attack
- Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
- Example of SMS interface verification code function implemented by ThinkPHP framework
- Practice of dynamic load balancing based on open source tars
- A flaw in R markdown: folders cannot have Chinese
- Istio FAQ: istio init crash
- How does the video networking / network penetration tool easynts permanently delete one of the devices?
- Pipeline shared library
- Pinduoduo press the user accelerator key
猜你喜欢

一文讲透植物内生菌研究怎么做 | 微生物专题

Dingding, Feishu, and enterprise wechat: different business approaches

Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer

How can a shell script (.Sh file) not automatically close or flash back after execution?

解析nc格式文件,GRB格式文件的依赖包edu.ucar.netcdfAll的api 学习

文本转语音功能上线,可以体验专业播音员的服务,诚邀试用

A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore

Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall
Cloud native database: the outlet of the database, you can also take off

MySQL foreign key impact
随机推荐
105. 简易聊天室8:使用 Socket 传递图片
What are the software prototyping tools?
【数据挖掘】期末复习(样卷题目+少量知识点)
Example of SMS interface verification code function implemented by ThinkPHP framework
巴比特 | 元宇宙每日必读:618成绩已然揭晓,在这份还算满意的答卷背后,数字藏品做出了多少贡献?...
Listed JD Logistics: breaking through again
IOMMU (VII) -vfio and mdev
[redisson] analysis of semaphore lock source code
Is it safe to open an account for how many new bonds you can apply for
Deep parsing and implementation of redis pub/sub publish subscribe mode message queue
Jupyter notebook service installation and startup
How to apply for new bonds is it safe to open an account
It's settled! Bank retail credit risk control just does it!
A good habit that makes your programming ability soar
Hardware enterprise website ranking, 8 commonly used processes
Mlife forum | microbiome and data mining
Sms service sms
Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer
Tencent released credit risk control results safely: it has helped banks lend more than 100 billion yuan
How to make secruecrt more productive