当前位置:网站首页>Leetcode 1218. Longest definite difference subsequence
Leetcode 1218. Longest definite difference subsequence
2022-06-24 12:50:00 【I'm not xiaohaiwa~~~~】
Give you an array of integers arr And an integer difference, Please find out and return to arr The length of the longest isochromatic subsequence in , The difference between adjacent elements in this subsequence is equal to difference .
Subsequence It means that without changing the order of the other elements , Remove from... By deleting some elements or not deleting any elements arr Derived sequence .
Example 1:
Input :arr = [1,2,3,4], difference = 1
Output :4
explain : The longest isochromatic subsequence is [1,2,3,4].
Example 2:
Input :arr = [1,3,5,7], difference = 1
Output :1
explain : The longest arithmetic subsequence is any single element .
Example 3:
Input :arr = [1,5,7,8,5,3,4,2,1], difference = -2
Output :4
explain : The longest isochromatic subsequence is [7,5,3,1].
Tips :
- 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;
}
};
边栏推荐
- Several common DoS attacks
- Kubernetes practical skill: entering container netns
- Difference between X12 830 and 862 messages
- SCRM, a breakthrough in the new consumption era
- 几种常见的DoS攻击
- A scheme for crawlers to collect public opinion data
- Another prize! Tencent Youtu won the leading scientific and technological achievement award of the 2021 digital Expo
- Codereview tool chain for micro medicine
- Discussion on redis communication protocol
- 短信服務sms
猜你喜欢

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

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

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

LVGL库入门教程 - 颜色和图像

解析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

MySQL 外键影响

Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
![[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve](/img/ec/6c6e3d878e2a05a6e7a4ca336ae134.jpg)
[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
随机推荐
Istio practical skills: using prism to construct multi version test services
Hardware enterprise website ranking, 8 commonly used processes
【2022国赛模拟】摆(bigben)——行列式、杜教筛
How to do research on plant endophytes? Special topic on Microbiology
【数据挖掘】期末复习(样卷题目+少量知识点)
105. simple chat room 8: use socket to transfer pictures
105. 简易聊天室8:使用 Socket 传递图片
Detailed explanation of the execution order of the expression and loop body in the for loop
As one of the bat, what open source projects does Tencent have?
[tke] GPU node NVIDIA Tesla driver reinstallation
[day ui] alert component learning
[log service CLS] Tencent cloud log service CLS accesses CDN
Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
Pipeline shared library
短信服務sms
Istio FAQ: istio init crash
如何高效的分析online.log
How can ffmpeg streaming to the server save video as a file through easydss video platform?
Post processing - deep camera deformation effects
What is the reason why the video intelligent analysis platform easycvr is locally controllable but the superior equipment cannot control the subordinate equipment?