参考代码:
class Solution {
public:
int longestSubsequence(vector<int>& arr, int difference) {
int n=arr.size();
unordered_map<int,int> hash;//nums[i]绑定dp[i]
hash[arr[0]]=1;
int ret=1;
for(int i=1;i<n;i++)
{
int a=arr[i];
int b=a-difference;
hash[a]=hash[b]+1;
ret=max(ret,hash[arr[i]]);
}
return ret;
}
};