2023-12-09 18:42:21 +08:00
< p > Given an array < code > nums< / code > of integers, return < em > the length of the longest arithmetic subsequence in< / em > < code > nums< / code > .< / p >
2022-03-27 20:46:41 +08:00
2023-12-09 18:42:21 +08:00
< p > < strong > Note< / strong > that:< / p >
< ul >
< li > A < strong > subsequence< / strong > is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.< / li >
< li > A sequence < code > seq< / code > is arithmetic if < code > seq[i + 1] - seq[i]< / code > are all the same value (for < code > 0 < = i < seq.length - 1< / code > ).< / li >
< / ul >
2022-03-27 20:46:41 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< pre >
< strong > Input:< / strong > nums = [3,6,9,12]
< strong > Output:< / strong > 4
2023-12-09 18:42:21 +08:00
< strong > Explanation: < / strong > The whole array is an arithmetic sequence with steps of length = 3.
2022-03-27 20:46:41 +08:00
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< pre >
< strong > Input:< / strong > nums = [9,4,7,2,10]
< strong > Output:< / strong > 3
2023-12-09 18:42:21 +08:00
< strong > Explanation: < / strong > The longest arithmetic subsequence is [4,7,10].
2022-03-27 20:46:41 +08:00
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:46:41 +08:00
< pre >
< strong > Input:< / strong > nums = [20,1,15,3,10,5,8]
< strong > Output:< / strong > 4
2023-12-09 18:42:21 +08:00
< strong > Explanation: < / strong > The longest arithmetic subsequence is [20,15,10,5].
2022-03-27 20:46:41 +08:00
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 2 < = nums.length < = 1000< / code > < / li >
< li > < code > 0 < = nums[i] < = 500< / code > < / li >
< / ul >