Class Solution
java.lang.Object
g1001_1100.s1027_longest_arithmetic_subsequence.Solution
1027 - Longest Arithmetic Subsequence.<p>Medium</p>
<p>Given an array <code>nums</code> of integers, return the <strong>length</strong> of the longest arithmetic subsequence in <code>nums</code>.</p>
<p>Recall that a <em>subsequence</em> of an array <code>nums</code> is a list <code>nums[i<sub>1</sub>], nums[i<sub>2</sub>], …, nums[i<sub>k</sub>]</code> with <code>0 <= i<sub>1</sub> < i<sub>2</sub> < … < i<sub>k</sub> <= nums.length - 1</code>, and that a sequence <code>seq</code> is <em>arithmetic</em> if <code>seq[i+1] - seq[i]</code> are all the same value (for <code>0 <= i < seq.length - 1</code>).</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [3,6,9,12]</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> The whole array is an arithmetic sequence with steps of length = 3.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [9,4,7,2,10]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The longest arithmetic subsequence is [4,7,10].</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [20,1,15,3,10,5,8]</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> The longest arithmetic subsequence is [20,15,10,5].</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 1000</code></li>
<li><code>0 <= nums[i] <= 500</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
longestArithSeqLength
public int longestArithSeqLength(int[] nums)
-