当前位置:网站首页>leetcode1218:最长定差子序列
leetcode1218:最长定差子序列
2022-04-22 06:08: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]。
思路一:动态规划
动态规划有几个典型特征,最优子结构、状态转移方程、边界、重叠子问题。
在最长定差子序列问题中:
假设 d p [ i ] dp[i] dp[i]为以 a r r [ i ] arr[i] arr[i]为结尾的最长等差子序列的长度,因此我们可以在 a r r [ i ] arr[i] arr[i]左侧找到满足 a r r [ j ] = a r r [ i ] − d arr[j]=arr[i]-d arr[j]=arr[i]−d,则此时 d p [ i ] = d p [ j ] + 1 dp[i]=dp[j]+1 dp[i]=dp[j]+1,其中 0 < j < i 0<j<i 0<j<i。
因此从数组元素自左向右遍历的过程中:
- 最优子结构为: d p [ j ] + 1 dp[j]+1 dp[j]+1
- 状态转移方程为: d p [ i ] = d p [ j ] + 1 → d p [ i ] = d p [ i − d ] + 1 dp[i]=dp[j]+1 \rightarrow dp[i]=dp[i-d]+1 dp[i]=dp[j]+1→dp[i]=dp[i−d]+1
- 边界为: d p [ 1 ] = 1 dp[1]=1 dp[1]=1
- **重叠问题:**等差子序列长度计算问题
- 最终答案: m a x ( d p [ i ] , d p [ j ] ) max(dp[i],dp[j]) max(dp[i],dp[j])
代码如下:
class Solution {
public int longestSubsequence(int[] arr, int difference) {
int result = 0;
Map<Integer,Integer> map = new HashMap<>();
for(int i : arr){
map.put(i,map.getOrDefault(i-difference,0)+1);
result = Math.max(result,map.get(i));
}
return result;
}
}
版权声明
本文为[浅浅望]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xpl_1620/article/details/121159859
边栏推荐
- 集成电路模拟版图入门-版图基础学习笔记(一)
- [review of Blue Bridge Cup] tree of life
- 协方差与协方差矩阵
- 矩阵的分解——LU分解
- Have you really done the right way to add stamp holes on PCB
- Stm32wb55 RTT based ble sample making process
- Application of can card in robot system
- Typec转HDMI 4K30HZ扩展芯片方案CS5261和CS5266设计参数及电路对比
- 6. What is ROS
- AG9310MCQ支持母座正反插Typec转hdmi投屏方案设计参考电路
猜你喜欢

MCIeCAN在工控机中的应用

沁恒CH573开发板上手

CAN透传记录云网关为工程机械赋能

What kind of design is sure?

Application of ring network redundant can optical transceiver in Baldwin fire alarm system

集成电路模拟版图入门-版图基础学习笔记(二)

Remote program upgrade scheme of transparent cloud gateway in construction machinery under epidemic environment

如何成为IC验证工程师?

HDMI2. Design circuit comparison between asw3642 and ts3dv642

STM32WB55 蓝牙协议栈运行流程解析
随机推荐
替代FE1.1S,MA8601,性价比高,中文方案,奇岩一级代理,HUB方案
Typec to HDMI + PD3 0 + U3 + U2 + SD / TF card reading expansion seven in one scheme design circuit | cs5266 + ma8621 design reference circuit
Stm32wb55 RTT based ble sample making process
HDMI2.0二进一出切换器方案ASW3642和TS3DV642设计电路对比
HDMI2. Design circuit comparison between asw3642 and ts3dv642
Handwritten numeral recognition in deep learning environment
Quantify the relationship between 911 calls and years from 2015 to 2017
The test posture should be rigorous
从 Spec.到芯片_(数字IC、模拟IC、FPGA/CPLD设计的流程及EDA工具)
模拟电路板调试前的准备工作_电路板的模拟检测
[MCUKeys] 一个通用的、灵活的、可配置的、可移植的按键事件处理的实现
替代 FE1.1s HUB讀卡主控芯片-MA8601
Replace Fe1 1s, ma8601, cost-effective, Chinese solution, Qiyan primary agent, Hub solution
腾讯云物联网-网关设备体验
MATLAB:女声转男声
The capacitor on PCBA is cracked and short circuited. Why is it the fault of design?
Is there any difference in the worst impedance processing you have encountered?
Oscilloscope can only measure waveform - the pattern is small
集成电路模拟版图入门-版图基础学习笔记(四)
Dcoker installation