<jupyter_start><jupyter_text>说明：    给定一个整数数组 A，返回 A 中最长等差子序列的长度。    回想一下，A 的子序列是列表 A[i_1], A[i_2], ..., A[i_k] 其中 0 <= i_1 < i_2 < ... < i_k <= A.length - 1。    并且如果 B[i+1] - B[i]( 0 <= i < B.length - 1) 的值都相同，那么序列 B 是等差的。 示例 1：    输入：[3,6,9,12]    输出：4    解释：         整个数组是公差为 3 的等差数列。示例 2：    输入：[9,4,7,2,10]    输出：3    解释：        最长的等差子序列是 [4,7,10]。示例 3：    输入：[20,1,15,3,10,5,8]    输出：4    解释：        最长的等差子序列是 [20,15,10,5]。 提示：    1、2 <= A.length <= 2000    2、0 <= A[i] <= 10000<jupyter_code>class Solution:
    def longestArithSeqLength(self, A) -> int:
        difs = set()
        max_count = -float('inf')
        for i in range(len(A)):
            for j in range(i+1, len(A)):
                count = 2
                dif = A[j] - A[i]
                if dif in difs:
                    continue
                difs.add(dif)
                count += self.helper(j, dif, A)
                max_count = max(count, max_count)
        return max_count
    
    def helper(self, idx, dif, A):
        if idx == len(A):
            return 0
        count, max_count = 0, 0
        for k in range(idx+1, len(A)):
            if A[k] - A[idx] == dif:
                count += self.helper(k, dif, A) + 1
            else:
                max_count = max(max_count, count)
                count = 0
        return count
from collections import defaultdict

class Solution:
    def longestArithSeqLength(self, A) -> int:
        dp = defaultdict(int) # dp表示 以 j 结尾，并且等差为 diff 的最大长度是多少
        for i in range(len(A)):
            for j in range(i):
                diff = A[i] - A[j]
                if dp[(j, diff)] == 0:
                    dp[(i, diff)] = 2
                else:
                    dp[(i, diff)] = dp[(j, diff)] + 1
        return max(dp.values())
solution = Solution()
solution.longestArithSeqLength([3,6,9,12])
a = [1, 2, 3]
print(a[1+1:])<jupyter_output>[3]
{'fraction_non_alphanumeric': 0.15082315454062667, 'fraction_numerical': 0.044078597981943704, 'mean_word_length': 1.5879120879120878, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '312611', 'license': 'Apache-2.0', 'n_tokens_mistral': 832, 'n_tokens_neox': 803, 'n_words': 180, 'path': 'Dynamic Programming/1029/1027. Longest Arithmetic Subsequence.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '1883'}