Leetcode 1087 Solution

This article provides solution to leetcode question 1087 (longest-arithmetic-subsequence)

https://leetcode.com/problems/longest-arithmetic-subsequence

Solution

class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = collections.defaultdict(int) ans = 0 for i in range(len(A)): for j in range(i): d = A[i] - A[j] dp[(d, i)] = dp[(d, j)] + 1 ans = max(dp[(d, i)], ans) return ans + 1