leetcode-1218-Longest Arithmetic Subsequence of Given Difference
问题
Given an integer array arr
and an integer difference
, return the length of the longest subsequence in arr
which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference
.
Example 1:
1 | Input: arr = [1,2,3,4], difference = 1 |
Example 2:
1 | Input: arr = [1,3,5,7], difference = 1 |
Example 3:
1 | Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2 |
Constraints:
1 <= arr.length <= 10^5
-10^4 <= arr[i], difference <= 10^4
分析
$dp[i]$ 表示以 $i-10000$ 结尾的序列的长度。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/28/leetcode-1218/
License: 知识共享署名-非商业性使用 4.0 国际许可协议