当前位置:网站首页>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;
}
};
边栏推荐
- Jenkins pipeline syntax
- Continuous testing | test process improvement: practice continuous testing within iterations in coding
- Sms service sms
- [highlights] summary of award-winning activities of Tencent cloud documents
- 《回归故里》阅读笔记
- pipeline groovy
- Discussion on redis communication protocol
- How to solve the problem that MBR does not support partitions over 2T, and lossless transfer to GPT
- A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore
- Istio practical skills: implement header based authorization
猜你喜欢

WPF从零到1教程详解,适合新手上路

MySQL 外键影响

Use the open source tool k8tz to gracefully set the kubernetes pod time zone
Cloud native database: the outlet of the database, you can also take off

On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"

Concept + formula (excluding parameter estimation)

Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall

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

Mlife forum | microbiome and data mining

How can a shell script (.Sh file) not automatically close or flash back after execution?
随机推荐
MySQL 外键影响
About me, a 19 line programmer
Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall
Example of SMS interface verification code function implemented by ThinkPHP framework
[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
Istio practical skills: using prism to construct multi version test services
National standard platform easygbs administrator assigns roles to sub users and troubleshooting of invalid channels
SMS SMS
Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
Cluster control management
I'm in Shenzhen. Where can I open an account? Is it safe to open an account online now?
A scheme for crawlers to collect public opinion data
Jupyter notebook service installation and startup
Kubernetes best practice: graceful termination
【数据库】期末复习(计科版)
Concentrate on research preparation, Tencent cloud, see you next year!
[database] final review (planning Edition)
How to configure the national standard platform easygbs neutral version?
Use txvideoeditor to add watermark and export video card at 99%? No successful failed callback?
Adjustment method of easynvr video platform equipment channel page display error