当前位置:网站首页>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;
}
};
边栏推荐
- [unity] instructions for beginners of textanimator plug-in
- Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
- Strong cache and negotiation cache in http
- Call face recognition exception
- [esp8266 - 01s] obtenir la météo, Ville, heure de Beijing
- Simpledateformat has thread safety problems in multi-threaded environments.
- Cryptography involved in IOT device end
- This time, thoroughly understand the SparseArray implementation principle
- Dive into deep learning - 1. Introduction
- 云原生行业应用崛起,从“可用”到“好用”有多远?
猜你喜欢
![[tool C] - lattice simulation test 2](/img/a2/0f9641332c9c13493ee8b3e568a294.png)
[tool C] - lattice simulation test 2

【Wwise】Wwise嵌入Unity后打包出现没有声音问题

Thesis reading (53):universal advantageous perturbations

QML类型:Loader

csdn涨薪秘籍之Jenkins集成allure测试报告全套教程

微信小程序startLocationUpdateBackground()简单实现骑手配送位置

【C工具】------点阵模拟测试2

Paper reading (54):deepfool: a simple and accurate method to four deep neural networks

Rancher2.6全新Monitoring快速入门

正则表达式使用图床
随机推荐
Counter attack and defense (2): counter strategy against samples
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
【故障公告】取代 memcached 的 redis 出现问题造成网站故障
Goframe framework: add tracing Middleware
esp8266-01s 不能连接华为路由器解决方法
How to design a seckill system - geek course notes
一元二次方程到规范场
Method of copying web page content and automatically adding copyright information (compatible with ie, Firefox and chrome)
New function! Qianfan magic pen apaas December capability monthly report
Video anomaly detection data set (shanghaitech)
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到
Redis 集群
Remote connection raspberry pie in VNC Viewer Mode
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
论文阅读 (52):Self-Training Multi-Sequence Learning with Transformer for Weakly Supervised Video Anomaly
VNC Viewer方式的远程连接树莓派
The battlefield of live broadcast e-commerce is not in the live broadcast room
论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
Kdevtmpfsi processing of mining virus -- Practice
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...