Leetcode 1087 Solution
This article provides solution to leetcode question 1087 (longest-arithmetic-subsequence)
Access this page by simply typing in "lcs 1087" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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