File tree 1 file changed +7
-5
lines changed
1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change 1
- from typing import List
2
1
"""
3
2
Longest Arithmetic Subsequence Problem: Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
4
3
8
7
- A sequence seq is arithmetic if seq[i + 1] - seq[i] are all the same value (for 0 <= i < seq.length - 1).
9
8
"""
10
9
11
- def longest_arithmetic_subsequence (nums : List [int ]) -> int :
10
+ def longest_arithmetic_subsequence (nums : list [int ]) -> int :
12
11
"""
13
12
Finds the length of the longest arithmetic subsequence in a given array of integers.
14
13
15
14
Parameters
16
15
----------
17
- nums : List [int]
16
+ nums : list [int]
18
17
The input array of integers.
19
18
20
19
Returns
@@ -40,8 +39,11 @@ def longest_arithmetic_subsequence(nums: List[int]) -> int:
40
39
if nums is None :
41
40
raise ValueError ("Input array cannot be None" )
42
41
43
- if len (nums ) <= 1 :
44
- return len (nums )
42
+ if len (nums ) == 0 :
43
+ return 0
44
+
45
+ if len (nums ) == 1 :
46
+ return 1
45
47
46
48
dp = [{} for _ in range (len (nums ))]
47
49
max_length = 2
You can’t perform that action at this time.
0 commit comments