Leetcode 1330 Solution

This article provides solution to leetcode question 1330 (longest-arithmetic-subsequence-of-given-difference)

https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference

Solution

class Solution:
    def longestSubsequence(self, arr: List[int], difference: int) -> int:
        m = collections.defaultdict(int)
        for v in arr:
            m[v] = m[v - difference] + 1 if v - difference in m else 1
        return max(m.values())