1218.cpp (328B)
1 class Solution { 2 public: 3 int longestSubsequence(const vector<int> &arr, int diff) { 4 unordered_map<int, int> dp; 5 6 int res = 1; 7 for (int n : arr) { 8 int before = dp.count(n - diff) ? dp[n - diff] : 0; 9 res = max(res, dp[n] = before + 1); 10 } 11 12 return res; 13 } 14 };