当前位置:网站首页>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;
}
};
边栏推荐
- WPF from zero to 1 tutorial details, suitable for novices on the road
- A good habit that makes your programming ability soar
- IOMMU (VII) -vfio and mdev
- Pipeline post instruction
- 99% of the students can't write good code because of this problem!
- JVM GC garbage collection detailed introduction quick check of learning notes
- Practice of dynamic load balancing based on open source tars
- Interesting erasure code
- 使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
- 微医CodeReview工具链
猜你喜欢

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

Opencv learning notes -- Separation of color channels and multi-channel mixing

【数据挖掘】期末复习(样卷题目+少量知识点)

Opencv learning notes - loading and saving images

Codereview tool chain for micro medicine

The text to voice function is available online. You can experience the services of professional broadcasters. We sincerely invite you to try it out

使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区

QT -- the qtabwidget supports dragging tabbar items

Use the open source tool k8tz to gracefully set the kubernetes pod time zone

How to do research on plant endophytes? Special topic on Microbiology
随机推荐
Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
How does the video networking / network penetration tool easynts permanently delete one of the devices?
Fbnet/fbnetv2/fbnetv3: Facebook's lightweight network exploration in NAS | lightweight network
Which commercial insurance endowment insurance is good? Ranking of commercial endowment insurance products in 2022
105. simple chat room 8: use socket to transfer pictures
It's settled! Bank retail credit risk control just does it!
Clickhouse uses distributed join of pose series
Concentrate on research preparation, Tencent cloud, see you next year!
Collation of related papers on root cause analysis
Opencv learning notes - matrix normalization normalize() function
Installing sqlserver extension PDO of PHP under Linux_ sqlsrv
[2021 techo youth dry goods sorting post, there is always one you are interested in]
From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms
Dingding, Feishu, and enterprise wechat: different business approaches
Jupyter notebook service installation and startup
解析nc格式文件,GRB格式文件的依赖包edu.ucar.netcdfAll的api 学习
Adjustment method of easynvr video platform equipment channel page display error
Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer
Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
WPF从零到1教程详解,适合新手上路