1218. Longest Arithmetic Subsequence of Given Difference
https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/
Python
class Solution:
def longestSubsequence(self, arr: List[int], difference: int) -> int:
mapper = {}
ans = 1
for num in arr:
cur = 1
if num - difference in mapper:
cur += mapper[num-difference]
ans = max(ans, cur)
mapper[num] = cur
return ans