Longest Arithmetic Sequence
Given an arrayA
of integers, return thelengthof the longest arithmetic subsequence inA
.
Recall that a_subsequence_ofA
is a listA[i_1], A[i_2], ..., A[i_k]
with0 <= i_1 < i_2 < ... < i_k <= A.length - 1
, and that a sequenceB
is_arithmetic_ifB[i+1] - B[i]
are all the same value (for0 <= i < B.length - 1
).
Example 1:
Example 2:
Example 3:
Note:
分析
对比Length of Longest Fibonacci Subsequence,本题input>1000, 双循环超时。
用dict的dp, 记载当前Index为尾数,该diff的最长长度,dp[index][diff] = dp[prev][diff]+1
本题dict用法, get()可以返回默认值,不同于dict[]没Key会报错
dict的Key可以不用tuple, 直接Index,diff
Last updated