1027. Longest Arithmetic Subsequence Medium

@problem@discussion
#Array#Hash Table#Binary Search#Dynamic Programming



1/**
2 * [1027] Longest Arithmetic Subsequence
3 *
4 * Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
5 * Recall that a subsequence of an array nums is a list nums[i1], nums[i2], ..., nums[ik] with 0 <= i1 < i2 < ... < ik <= nums.length - 1, and that a sequence seq is arithmetic if seq[i+1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [3,6,9,12]
10 * Output: 4
11 * Explanation: 
12 * The whole array is an arithmetic sequence with steps of length = 3.
13 * 
14 * Example 2:
15 * 
16 * Input: nums = [9,4,7,2,10]
17 * Output: 3
18 * Explanation: 
19 * The longest arithmetic subsequence is [4,7,10].
20 * 
21 * Example 3:
22 * 
23 * Input: nums = [20,1,15,3,10,5,8]
24 * Output: 4
25 * Explanation: 
26 * The longest arithmetic subsequence is [20,15,10,5].
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	2 <= nums.length <= 1000
32 * 	0 <= nums[i] <= 500
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/longest-arithmetic-subsequence/
38// discuss: https://leetcode.com/problems/longest-arithmetic-subsequence/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn longest_arith_seq_length(nums: Vec<i32>) -> i32 {
44        0
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_1027() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.