Class Solution
- java.lang.Object
-
- g1001_1100.s1027_longest_arithmetic_subsequence.Solution
-
public class Solution extends Object
1027 - Longest Arithmetic Subsequence.Medium
Given an array
nums
of integers, return the length of the longest arithmetic subsequence innums
.Recall that a subsequence of an array
nums
is a listnums[i1], nums[i2], …, nums[ik]
with0 <= i1 < i2 < … < ik <= nums.length - 1
, and that a sequenceseq
is arithmetic ifseq[i+1] - seq[i]
are all the same value (for0 <= i < seq.length - 1
).Example 1:
Input: nums = [3,6,9,12]
Output: 4
Explanation: The whole array is an arithmetic sequence with steps of length = 3.
Example 2:
Input: nums = [9,4,7,2,10]
Output: 3
Explanation: The longest arithmetic subsequence is [4,7,10].
Example 3:
Input: nums = [20,1,15,3,10,5,8]
Output: 4
Explanation: The longest arithmetic subsequence is [20,15,10,5].
Constraints:
2 <= nums.length <= 1000
0 <= nums[i] <= 500
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
longestArithSeqLength(int[] nums)
-