Skip to content

Latest commit

 

History

History
18 lines (16 loc) · 434 Bytes

1027.md

File metadata and controls

18 lines (16 loc) · 434 Bytes

1027. Longest Arithmetic Subsequence

Solution 1 (time O(n^2), space O(n^2))

# Dynamic programming
class Solution(object):
    def longestArithSeqLength(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        dp = {}
        for j in range(len(A)):
            for i in range(j):
                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1
        return max(dp.values())