1. 最长定差子序列
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/693af256aa379a0cc205e4b53c473870.png)
(1)DP(数组)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bd754d95a5f76be5018c3bcae14b5e9b.png)
class Solution {
public int longestSubsequence(int[] arr, int difference) {
int ans = 0;
int[] dp = new int[40001];
for (int num : arr) {
dp[num + 20000] = dp[num + 20000 - difference] + 1;
ans = Math.max(ans, dp[num + 20000]);
}
return ans;
}
}