当前位置:网站首页>Leetcode 1218. 最长定差子序列(提供一种思路)
Leetcode 1218. 最长定差子序列(提供一种思路)
2022-06-23 17:23: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;
for(int i=0;i<arr.size();i++)
{
int start=arr[i];
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;
}
};
边栏推荐
- console. Log() is an asynchronous operation???
- 6、VLAN
- Alien world, real presentation, how does the alien version of Pokemon go achieve?
- Wechat applet startlocationupdatebackground() simply realizes rider distribution location
- Revil - blackmail Virus Emergency Response
- Cryptography involved in IOT device end
- Thesis reading (53):universal advantageous perturbations
- Redis 集群
- How to make validity table
- 云原生行业应用崛起,从“可用”到“好用”有多远?
猜你喜欢

Self training multi sequence learning with transformer for weakly supervised video animation

论文阅读 (53):Universal Adversarial Perturbations

对抗攻击与防御 (1):图像领域的对抗样本生成

【ESP8266-01s】获取天气,城市,北京时间

Redis cluster

Rancher2.6全新Monitoring快速入门

Wiley- Open Science Joint Symposium of the documentation and information center of the Chinese Academy of Sciences, lecture 2: open access journal selection and paper submission

如何利用好每天的时间高效复习?

论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly

TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
随机推荐
Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
Regular expression use graph bed
实用电路分析3
README
How do I write a small program that can automatically edit new year greetings
[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing
【C工具】------点阵模拟测试2
Call face recognition exception
Reading papers (51):integration of a holonic organizational control architecture and multiobjective
深入理解 padding
Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
Troubleshooting and modification process of easycvr interface dislocation in small screen
How to make a badge
Imeta | Nannong shenqirong team released microbial network analysis and visualization R package ggclusternet
Strong cache and negotiation cache in http
"Tribute to a century old master, collect pocket book tickets"
Torch learning (I): environment configuration
Illustration of mongodb cluster deployment principle (3)
Thesis reading (53):universal advantageous perturbations