当前位置:网站首页>Leetcode 1218. Longest definite difference subsequence (providing an idea)
Leetcode 1218. Longest definite difference subsequence (providing an idea)
2022-06-23 18:25: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;
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;
}
};
边栏推荐
- Deploy LNMP environment and install Typecho blog
- Wiley-中国科学院文献情报中心开放科学联合研讨会第二讲:开放获取期刊选择及论文投稿...
- [sword finger offer] 46 Translate numbers into strings
- TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
- New function! Qianfan magic pen apaas December capability monthly report
- 6 steps to teach you financial data mining preprocessing
- Wechat applet startlocationupdatebackground() simply realizes rider distribution location
- [sword finger offer] 45 Arrange the array into the smallest number
- Redis 集群
- Reading papers (51):integration of a holonic organizational control architecture and multiobjective
猜你喜欢

Practical circuit analysis 3

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

esp8266-01s 不能连接华为路由器解决方法

Customer service system building tutorial_ Installation and use mode under the pagoda panel_ Docking with official account_ Support app/h5 multi tenant operation

【Unity】插件TextAnimator 新手使用说明

QML type: Loader

【 Huazhong University of Science and technology】 Data Sharing for retest of the initial Examination

Video anomaly detection data set (shanghaitech)

知道创宇:内容向善,AI+人工赋能

Landing of global organizational structure control
随机推荐
Vulnerability in McAfee epolicy orchestrator
CSDN salary increase secret script: Jenkins integrated allure test report complete tutorial
Redis 集群
Redis cluster
客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...
Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
A set of code to launch seven golang web frameworks at the same time
Asynchronous or thread pool
[learning notes] tidb learning notes (III)
VNC Viewer方式的远程连接树莓派
TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
Kerberoasting without SPN
leetcode刷题:哈希表07 (三数之和)
实用电路分析3
Remote connection raspberry pie in VNC Viewer Mode
聊一聊数据库的行存与列存
Paper reading (48):a Library of optimization algorithms for organizational design
视频异常检测数据集 (ShanghaiTech)
Cryptography involved in IOT device end
This time, thoroughly understand the SparseArray implementation principle