Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: d = {} dp = {i:defaultdict(int) for i in range(len(A))} mx = 0 for i in range(len(A)): if A[i] not in d: d[A[i]] = 1 for j in range(0, i): ap = A[i] + -A[j] if A[i] + -ap in d: dp[i][ap] = dp[j][ap] + 1 mx = max(mx, dp[i][ap]) return mx+1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: ans = 0 dp = {} #dp[i,d] = max length subseq up to i with diff d for i in range(len(A)): for j in range(i): d = A[i] - A[j] if (j,d) in dp: dp[i,d] = dp[j,d] + 1 else: dp[i,d] = 2 ans = max(ans, dp[i,d]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = [{} for _ in range(len(A))] res = 0 for i in range(len(A)): for j in range(i): x = A[i] - A[j] dp[i][x] = max(dp[j][x]+1 if x in dp[j] else 0, 2 if x not in dp[i] else dp[i][x]) res = max(dp[i][x],res) #print(dp) return res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = [] for i, x in enumerate(A): nd = collections.defaultdict(int) dp.append(nd) for j in range(i): diff = x - A[j] dp[i][diff] = dp[j][diff] + 1 return max(max(y.values()) for y in dp) + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # dp = defaultdict(int) # for i in range(len(A)): # for j in range(i+1, len(A)): # d = A[j] - A[i] # if (i, d) in dp: # dp[j, d] = dp[i, d] + 1 # else: # dp[j, d] = 2 # return max(dp.values()) dp = {} for i, Ai in enumerate(A): for j in range(i+1, len(A)): b = A[j] - Ai if (i,b) not in dp: dp[j,b] = 2 else : dp[j,b] = dp[i,b] + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution2: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = [[0]*501 for i in range(n)] max_val = 0 for i in range(n): for j in range(i): dif = A[i] - A[j] dp[i][dif] = max(dp[i][dif], dp[j][dif] + 1) max_val = max(dp[i][dif], max_val) #print(dp) return max_val + 1 class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = collections.defaultdict(dict) max_val = 0 for i in range(n): for j in range(i): dif = A[i] - A[j] dp[dif].setdefault(i, 0) dp[dif][i] = dp[dif].get(j,0) + 1 max_val = max(dp[dif][i], max_val) #print(dp) return max_val + 1 class Solution1: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = [collections.defaultdict(int) for _ in range(n)] res = 0 for i in range(1, n): for j in range(0, i): diff = A[i]-A[j] dp[i][diff] = dp[j][diff] + 1 res = max(res, dp[i][diff]) return res + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, nums: List[int]) -> int: if not nums: return 0 graph = collections.defaultdict(lambda: collections.defaultdict(int)) res = 0 for i in range(1, len(nums)): for j in range(i): diff = nums[i] - nums[j] prev_diffs = graph[j] prev_diff = prev_diffs[diff] graph[i][diff] = prev_diff + 1 res = max(res, graph[i][diff]) return res + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} ans = 0 for i in range(len(A)): for j in range(i+1, len(A)): b = A[j] - A[i] if (i,b) not in dp: dp[(j,b)] = 2 else: dp[(j,b)] = dp[(i,b)] + 1 ans = max(ans, dp[(j, b)]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = defaultdict(int) for i in range(1, len(A)): for j in range(i): d = A[i] - A[j] if (j, d) in dp: dp[i, d] = dp[j, d] + 1 else: dp[i, d] = 2 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) tracker = {} max_length = 1 for i in range(n): for j in range(i): diff = A[i] - A[j] if (diff, j) in tracker: tracker[(diff, i)] = tracker[(diff, j)] + 1 else: tracker[(diff, i)] = 2 max_length = max(max_length, tracker[(diff, i)]) return max_length
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if not A: return 0 if len(A) <= 2: return len(A) dic = {} for idx, val in enumerate(A): for j in range(idx + 1, len(A)): diff = A[j] - val if (diff, idx) in dic: dic[diff, j] = dic[(diff, idx)] + 1 else: dic[diff, j] = 2 return max(dic.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): diff = A[j] - A[i] dp[(j, diff)] = dp.get((i, diff), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = [[1 for i in range(1001)] for j in range(len(A))] lastIndex = {} ans = 1 for i,num in enumerate(A): for j in range(1001): commonDiff = j-500 lastNumIndex = lastIndex.get(num-commonDiff,-1) if(lastNumIndex >= 0 and lastNumIndex < i): dp[i][j] = dp[lastNumIndex][j] + 1 ans = max(ans,dp[i][j]) if(num not in lastIndex): lastIndex[num] = -1 lastIndex[num] = i return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: cache = dict() for i in range(len(A)): for j in range(i + 1, len(A)): diff = A[j] - A[i] if (i, diff) not in cache: cache[(j, diff)] = 2 else: cache[(j, diff)] = 1 + cache[(i, diff)] return max(cache.values()) ''' dp[i][d] = longest subsequence ending at i with difference d dp[j][d] = 1 + max( dp[j][A[j] - A[i]] ) for j < i '''
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: #O(n^2) Bottom-up DP def longestArithSeqLength(self, A): dp = [] ans,n = 2,len(A) for i in range(n): dp.append(dict()) for j in range(i-1,-1,-1): diff = A[i]-A[j]# create a start with A[i],A[j] if diff not in dp[i]: dp[i][diff] = 2 # going backward to make sure information can be used! if diff in dp[j]: dp[i][diff] = max(dp[j][diff]+1,dp[i][diff]) ans = max(ans,dp[i][diff]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: M=max(A)+1 dp=[] for i in range(len(A)): temp=[1]*(M*2) dp.append(temp) m=0 for i in range(len(A)): for j in range(i): delta=A[i]-A[j] k=delta+M # print(k,M) dp[i][k]=dp[j][k]+1 m=max(m,dp[i][k]) # m=0 # for i in range(2*M): # for j in range(len(A)): # m=max(dp[j][i],m) # # print(dp) return m
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # let dp[i][k] is the length of the arith seq with step size k # range is -500, 500 n = len(A) m = 1001 dp = [ [1] * (m+1) for _ in range(n)] # can replace it by a dict, will be quicker ans = float('-inf') for i in range(1,n): for k in range(i): dp[i][A[i]-A[k]+500] = max(dp[i][A[i]-A[k]+500], 1 + dp[k][A[i]-A[k]+500]) ## a_i - a_{i-1} = j ans = max(max(dp[i]),ans) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {}; n = len(A); res = 2 for i in range(n): for j in range(i+1, n): b = A[j] - A[i] if (i,b) not in dp: dp[j,b] = 2 else : dp[j,b] = dp[i,b] + 1 res = max(res, dp[j,b]) return res #return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: cache = {} ans = 0 for i in range(len(A)): for j in range(i+1, len(A)): # print(cache) diff = A[j] - A[i] if (i, diff) in cache: cache[j, diff] = cache[i, diff] + 1 ans = max(cache[j, diff], ans) else: cache[j, diff] = 2 ans = max(cache[j, diff], ans) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp, n, ans = {}, len(A), 0 for i in range(n): for j in range(i+1, n): diff = A[j] - A[i] dp[(j, diff)] = dp.get((i, diff), 1) + 1 ans = max(ans, dp[(j, diff)]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = {} mostFreq = -1 for i in range(1,n): for j in range(i-1, -1,-1): diff = A[i] - A[j] prevVal = 1 if (i,diff) in dp: prevVal = dp[ (i,diff)] if (j,diff) in dp: dp[(i,diff)] = max(dp[(j,diff)], prevVal-1) + 1 else: if (i,diff) not in dp: dp[(i,diff)] = 1 # mostFreq = max(dp[(i,diff)], mostFreq) ret = -1 for k,v in dp.items(): ret = max(ret,v) return ret+1 return mostFreq+1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A): dp = collections.defaultdict(int) mx = 0 for i in range(len(A)): for j in range(i): diff = A[i] - A[j] j_key = (j, diff) i_key = (i, diff) dp[i_key] = max(2, dp[j_key]+1) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} n = len(A) res = 2 for i in range(n): for j in range(i): d = A[i] - A[j] dp[d, i] = dp.get((d, j), 1) + 1 res = max(res, dp[d, i]) return res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if A is None or not A: return 0 N = len(A) f = [{} for _ in range(N + 1)] ret = 0 for i in range(2, N + 1): for j in range(1, i): diff = A[i - 1] - A[j - 1] if diff in f[j]: f[i][diff] = f[j][diff] + 1 else: f[i][diff] = 2 ret = max(ret, f[i][diff]) return ret
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = {} for j in range(1,n): for i in range(j): diff = A[j] - A[i] dp[j,diff] = max(dp.get((j,diff), 2), dp.get((i,diff),1)+1) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} res = 0 for i in range(len(A)): for j in range(0, i): d = A[i] - A[j] dp[i, d] = dp.get((j, d), 1) + 1 res = max(res, dp[i, d]) return res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # DP? if len(A) <= 2: return len(A) DP = [{} for i in range(len(A))] ret = 0 for i in range(1, len(A)): # j < i for j in range(i): diff = A[i] - A[j] l = 0 if diff in DP[j]: l = DP[j][diff] + 1 else: l = 2 # A[j] and A[i] new_longest = max(l, DP[i].get(diff, 0)) ret = max(ret, new_longest) DP[i][diff] = new_longest return ret
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} m = 0 for i in range(len(A)): for j in range(i): d = A[i] - A[j] j1 = (j,d) i1 = (i,d) if dp.get(j1) != None: dp[i1] = 1 + dp[j1] else: dp[i1] = 1 m = max(m,dp[i1]) return m+1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: count = 0 memo = defaultdict(int) for i in range(len(A)): for j in range(i+1,len(A)): num1 = A[i] num2 = A[j] diff = A[j] - num1 val = memo[(i,diff)] + 1 memo[(j, diff)] = val count = max(val, count) return count + 1 if count > 0 else count
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i+1, len(A)): b = A[j] - A[i] if (i,b) not in dp: dp[(j,b)] = 2 else: dp[(j,b)] = dp[(i,b)] + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: #natural algorithm is O(n^3) by test for each i<j all remaining sequence #O(n^2) Bottom-up DP def longestArithSeqLength(self, A): dp = [] ans,n = 2,len(A) for i in range(n): dp.append(dict()) for j in range(i-1,-1,-1): diff = A[i]-A[j]# create a start with A[i],A[j] if diff not in dp[i]: dp[i][diff] = 2 # going backward to make sure information can be used! if diff in dp[j]: dp[i][diff] = max(dp[j][diff]+1,dp[i][diff]) ans = max(ans,dp[i][diff]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i+1, len(A)): dp[(j, A[j]- A[i])] = dp.get((i, A[j] - A[i]),1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = dict() max_val = 0 for i in range(len(A)): for j in range(i+1, len(A)): dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1 if dp[j, A[j] - A[i]] > max_val: max_val = dp[j, A[j] - A[i]] return max_val
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # table[index][diff] equals to the length of # arithmetic sequence at index with difference diff. table = dict() max_v = 0 for i in range(len(A)): for j in range(i+1,len(A)): _diff = A[j] - A[i] if (i,_diff) in table.keys(): table[j,_diff] = table[i,_diff] + 1 else: table[j,_diff] = 2 # the first diff # will corrspond to two values [v1,v2] max_v = max(max_v,table[j,_diff]) return max_v
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(1, len(A)): for j in range(i): diff = A[i]-A[j] dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1) # print(dp) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: mp = defaultdict(list) for i, a in enumerate(A): mp[a].append(i) @lru_cache(None) def dp(i, diff=None): res = 1 if diff is None: for j in range(i): res = max(res, 1+ dp(j, A[i] - A[j])) else: for j in mp[A[i] - diff]: if j < i: res = max(res, 1+ dp(j, diff)) return res return max([dp(i) for i in range(len(A))])
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: ''' dp = {} for i in xrange(len(A)): for j in xrange(i + 1, len(A)): dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1 return max(dp.values()) ''' l=len(A) dp={} c=collections.defaultdict(list) for i in range(l-1): for j in range(i+1,l): #c[(i,j)]=A[i]-A[j] c[i].append(A[i]-A[j]) dp[j,A[j]-A[i]]=dp.get((i,A[j]-A[i]),1)+1 #print(c) return max(dp.values()) res=2 #for i in range(l-1): #analyze c[i]
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} n = len(A) for i in range(n): for j in range(i+1,n): diff = A[i]-A[j] dp[(diff, j)] = dp.get((diff,i), 1) + 1 #print(dp) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A): max_len, dp = 0, [{} for _ in range(len(A))] # index_of_array: {diff: curr_max_len} for i in range(1, len(A)): for j in range(0, i): diff = A[i] - A[j] if diff in dp[j]: dp[i][diff] = dp[j][diff] + 1 else: dp[i][diff] = 2 max_len = max(max_len, dp[i][diff] ) return max_len
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} # key = (index, diff), value = len of sequences for i in range(len(A)): for j in range(i + 1, len(A)): curr_diff = A[j] - A[i] dp[(j, curr_diff)] = dp.get((i, curr_diff), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = [{} for i in range(len(A))] res = 0 for i in range(len(A)): for j in range(i): # in the case that we are attaching to a single element if A[i]-A[j] in dp[j]: dp[i][A[i]-A[j]] = max(2, 1 + dp[j][A[i]-A[j]]) else: dp[i][A[i]-A[j]] = 2 res = max(res, dp[i][A[i]-A[j]]) return res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = collections.defaultdict(int) for i in range(len(A)): for j in range(i): diff = A[i] - A[j] dp[i, diff] = max(2, dp[j, diff] + 1) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) if n < 3: return n dp = defaultdict(lambda: defaultdict(int)) res = 2 for i in range(1, n): for j in range(i): gap = A[i] - A[j] dp[i][gap] = max(dp[j][gap] + 1, 2) res = max(res, dp[i][gap]) return res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = defaultdict(int) for i in range(len(A)): for j in range(i+1, len(A)): d = A[j] - A[i] if (i, d) in dp: dp[j, d] = dp[i, d] + 1 else: dp[j, d] = 2 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
#[Runtime: 4284 ms, faster than 10.74%] DP #O(N^2) #NOTE: diff can be either positive or negative #f[i]: the longest length of arithmetic subsequences who takes A[i] as the tail. #f[i] = defaultdict(lambda: 1) #f[i] = {diff: longest length} #f[i] = max(f[i][d], f[j][d] + 1) for j < i and d:=A[i]-A[j] from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = defaultdict(lambda: 1) for i, a in enumerate(A): for j in range(i): diff = A[i] - A[j] if dp[i, diff] < dp[j, diff] + 1: dp[i, diff] = dp[j, diff] + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: count = 0 memo = defaultdict(int) for i in range(len(A)): for j in range(i+1,len(A)): num1 = A[i] num2 = A[j] diff = A[j] - num1 val = memo[(num1,diff,i)] + 1 memo[(num2, diff,j)] = val count = max(val, count) return count + 1 if count > 0 else count
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if not A: return 0 dp = collections.defaultdict(int) for i in range(len(A)): for j in range(i+1, len(A)): dp[(j, A[j]-A[i])] = dp[(i, A[j]-A[i])] + 1 return max(dp.values()) + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: # [9, 4, 7, 2, 10] # 1 2 2 2 3 # (4, 5): 2 # (7, 2): 2 # (7, 3): 2 # (2, 7): 2 # (2, 2): 2 # (2, 5): 2 # (10, 1): 2 # (10, 6): 2 # (10, 3): 3 # (10, 8): 2 # store the last val in a sequence formed, its difference and length # [24, 13, 1, 100, 0, 94, 3, 0, 3] result = 2 # (13, 11): 2 # (1, 23): 2 # (1, 12): 2 # (100, 86): 2 # (100, 87): 2 # (100, 99): 2 # (0, -24): 2 # (0, -13): 2 # (0, -1): 2 # (0, -100): 2 # (94, 70): 2 # (94, 81): 2 # (94, 93): 2 # (94, -6): 2 # (94, 94): 2 # (3, -11): 2 # (3, -10): 2 # (3, -2): 2 # (3, -97): 2 # (3, 3, 7): 2 # (3, -91): 2 def longestArithSeqLength(self, A: List[int]) -> int: seqs = {} # stores mapping of (last_num_in_seq, diff) to length result = 2 for i in range(1, len(A)): for j in range(i): diff = A[i] - A[j] if (A[j], diff, j) in seqs: seqs[(A[i], diff, i)] = seqs[(A[j], diff, j)] + 1 else: seqs[(A[i], diff, i)] = 2 result = max(result, seqs[(A[i], diff, i)]) return result
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = list(dict() for i in range(len(A))) maxsize = 0 for i in range(1,len(A)): for j in range(0,i): diff = A[j] - A[i] if(diff in dp[j]): dp[i][diff] = dp[j][diff] + 1 else: dp[i][diff] = 1 maxsize = max(maxsize, dp[i][diff]) return maxsize + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp, ans = [], 2 for i in range(len(A)): dp.append({}) for j in range(i-1, -1, -1): dif = A[i]-A[j] if dif not in dp[i]: dp[i][dif] = 2 if dif in dp[j]: dp[i][dif] = max(dp[j][dif]+1, dp[i][dif]) ans = max(ans, dp[i][dif]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i): dp[i, A[i] - A[j]] = dp.get((j, A[i] - A[j]), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A)==0: return 0 ld=[{} for i in range(len(A))] ans=0 ind=-1 for i in range(1,len(A)): for j in range(i-1,-1,-1): diff=A[i]-A[j] if diff in ld[j]: # print(i,j,diff,ld[j][diff]) if diff in ld[i]: ld[i][diff]=max(ld[j][diff]+1,ld[i][diff]) else: ld[i][diff]=ld[j][diff]+1 else: if diff in ld[i]: ld[i][diff]=max(2,ld[i][diff]) else: ld[i][diff]=2 if ld[i][diff]>ans: ind=i ans=max(ans,ld[i][diff]) # print(ind,ld[ind],A[ind],len(A)) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: maxd = max(A) - min(A) dp = [[1 for j in range(2*maxd+1)] for i in range(len(A))] maxv = 1 for i in range(1, len(dp)): for j in range(i-1, -1, -1): diff = A[i]- A[j] dp[i][diff+maxd] = max(dp[i][diff+maxd], dp[j][diff+maxd] + 1) maxv = max(maxv, dp[i][diff+maxd]) return maxv
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A) <= 2: return len(A) idx_diff_to_longest = {} longest = 0 for i in range(1, len(A)): for j in range(i): diff = A[i] - A[j] prev_len = idx_diff_to_longest[(j, diff)] if (j, diff) in idx_diff_to_longest else 1 idx_diff_to_longest[(i, diff)] = prev_len + 1 longest = max(longest, prev_len + 1) return longest
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: A_indices = defaultdict(list) for i, a in enumerate(A): A_indices[a].append(i) lengths = defaultdict(lambda: 2) best = 2 for i in range(len(A) - 3, -1, -1): for j in range(i + 1, len(A) - 1): if 2 * A[j] - A[i] in A_indices: indices = A_indices[2 * A[j] - A[i]] # find earliest occurrence of 2 * A[j] + A[i] after j if indices[-1] <= j: continue if indices[0] > j: r = 0 else: l = 0 r = len(indices) - 1 while l < r - 1: mid = (l + r) // 2 if indices[mid] <= j: l = mid else: r = mid lengths[i, j] = 1 + lengths[j, indices[r]] best = max(best, lengths[i, j]) return best
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: #这题bottom up写起来快? dp = defaultdict(lambda: 1) #default 是 1, 自己肯定是自己的seq for i in range(len(A)): for j in range(i): #之前的 diff = A[i] - A[j] dp[(i, diff)] = 1+ dp[(j, diff)]#加速 这里不需要用max来取因为是从前往后遍历,最后valid的diff肯定是最大的 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: # [9, 4, 7, 2, 10] # 1 2 2 2 3 # (4, 5): 2 # (7, 2): 2 # (7, 3): 2 # (2, 7): 2 # (2, 2): 2 # (2, 5): 2 # (10, 1): 2 # (10, 6): 2 # (10, 3): 3 # (10, 8): 2 # store the last val in a sequence formed, its difference and length # [24, 13, 1, 100, 0, 94, 3, 0, 3] result = 2 # (13, 11): 2 # (1, 23): 2 # (1, 12): 2 # (100, 86): 2 # (100, 87): 2 # (100, 99): 2 # (0, -24): 2 # (0, -13): 2 # (0, -1): 2 # (0, -100): 2 # (94, 70): 2 # (94, 81): 2 # (94, 93): 2 # (94, -6): 2 # (94, 94): 2 # (3, -11): 2 # (3, -10): 2 # (3, -2): 2 # (3, -97): 2 # (3, 3, 7): 2 # (3, -91): 2 def longestArithSeqLength(self, A: List[int]) -> int: seqs = {} # stores mapping of (last_num_in_seq, diff) to length result = 2 for i in range(1, len(A)): for j in range(0, i): diff = A[i] - A[j] if (A[j], diff, j) in seqs: seqs[(A[i], diff, i)] = seqs[(A[j], diff, j)] + 1 else: seqs[(A[i], diff, i)] = 2 result = max(result, seqs[(A[i], diff, i)]) return result
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import Counter class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A)<=2: return len(A) dp = [Counter() for _ in range(len(A))] ans = 2 for i in range(len(A)): for j in range(i+1,len(A),1): diff = A[j]-A[i] if dp[i][diff] != 0: dp[j][diff] = dp[i][diff] + 1 else: dp[j][diff] = 2 ans = max(ans, dp[j][diff]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} n = len(A) for i in range(n): for j in range(i+1, n): dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: d = [{} for _ in range(len(A))] longest = 0 for i in range(1, len(A)): for j in range(i): diff = A[i] - A[j] d[i][diff] = max(d[i].get(diff, 0), 1 + d[j].get(diff, 1)) longest = max(longest, d[i][diff]) return longest
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) if n < 3: return n counts = [{} for _ in A] max_count = 0 for i in range(0, len(A)): for j in range(0, i): diff = A[i] - A[j] counts[i][diff] = max(counts[i].get(diff, 1), counts[j].get(diff, 1) + 1) max_count = max(max_count, counts[i][diff]) return max_count
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: N = len(A) dp = defaultdict(lambda: 1) for j in range(N): nxt = defaultdict(lambda: 1) for i in range(j): y, z = A[i], A[j] d = delta = z - y nxt[z, d] = max(nxt[z, d], dp[y, d] + 1) dp.update(nxt) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i+1, len(A)): dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1; return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, nums: List[int]) -> int: dp = defaultdict(int) n = len(nums) max_val = 0 for i in range(n-1): for j in range(i+1, n): diff = nums[i] - nums[j] dp[(j, diff)] = 1 + dp[(i, diff)] max_val = max(max_val, dp[(j,diff)]) return max_val + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(0, i): dp[i, A[i] - A[j]] = dp.get((j, A[i] - A[j]), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: result = 1 n = len(A) umap = [dict() for i in range(n)] for i in range(1, n): for j in range(i): diff = A[i] - A[j] jval = (umap[j][diff] if diff in umap[j] else 0) if diff not in umap[i]: umap[i][diff] = 0 umap[i][diff] = max(umap[i][diff], jval + 1) result = max(result, umap[i][diff]) return result+1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: d = [{} for _ in range(len(A))] longest = 0 for i in range(1, len(A)): for j in range(i): diff = A[i] - A[j] d[i][diff] = max(d[i].get(diff, 0), 1 + d[j].get(diff, 1)) longest = max(longest, d[i][diff]) return longest
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: N = len(A) dp = defaultdict(lambda: 1) for j in range(N): dp2 = defaultdict(lambda: 1) for i in range(j): y, z = A[i], A[j] d = delta = z - y dp2[z, d] = max(dp2[z, d], dp[y, d] + 1) dp.update(dp2) # for k, v in dp.items(): # print(k, v) # print() return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k dp = dict() res = 0 for i in range(1, len(A)): for prev_i in range(i): step = A[i] - A[prev_i] prev_step = dp[(prev_i, step)] if (prev_i, step) in dp else 1 dp[(i, step)] = prev_step + 1 res = max(res, dp[(i, step)]) return res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: A_indices = defaultdict(list) for i, a in enumerate(A): A_indices[a].append(i) # lengths (i, d) longest arithmetic subsequence starting at i # with difference d lengths = defaultdict(lambda: 1) best = 0 for i in range(len(A) - 2, -1, -1): for j in range(len(A) - 1, i, -1): diff = A[j] - A[i] lengths[i, diff] = lengths[j, diff] + 1 best = max(best, lengths[i, diff]) return best
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A)<=2: return len(A) dp = [{} for _ in range(len(A))] dp[1][A[1]-A[0]] = 2 maxLen = 0 for i in range(2, len(A)): for j in range(i): diff = A[i]-A[j] dp[i][diff] = max(dp[i][diff] if diff in dp[i] else 0, dp[j][diff]+1 if diff in dp[j] else 2) maxLen = max(maxLen, dp[i][diff]) return maxLen
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i): d = A[j] - A[i] if (j, d) in dp: dp[(i, d)] = dp[(j, d)] + 1 else: dp[(i, d)] = 2 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A): dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): if (i, A[j] - A[i]) in dp: dp[(j, A[j] - A[i])] = dp[(i, A[j] - A[i])] + 1 else: dp[(j, A[j] - A[i])] = 2 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
# dict.get(key,number) return number if get None # 用dict 紀錄 dict[(id,diff)]: count # dict[(new id,diff)] = dict[(old id,diff)] || 1 + 1 class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): x = dp.get((i, A[j] - A[i]), 1) # print(x) dp[j, A[j] - A[i]] = x + 1 # print(dp) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(1, len(A)): for j in range(i): diff = A[i]-A[j] dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) dp = [{} for i in range(n)] result = 0 for i, a in enumerate(A): for j in range(i): l = dp[j].get(a - A[j], 1) + 1 dp[i][a - A[j]] = max(dp[i].get(a - A[j], 0) ,l) result = max(result, l) return result
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if not A: return 0 result = 0 N = len(A) d = collections.defaultdict(int) for i in range(1,N): for j in range(i): diff = A[i] - A[j] d[(i,diff)] = d[(j,diff)] +1 result = max(result, d[(i,diff)] ) return result+1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: diff_dict = {} max_ = 0 for i in range(len(A)): diff_dict[i] = {} for j in range(i): diff = A[i] - A[j] if diff in diff_dict[j]: diff_dict[i][diff] = diff_dict[j][diff] + 1 else: diff_dict[i][diff] = 1 max_ = max(max_, diff_dict[i][diff]) return max_ + 1 # diff_dict = {} # max_ = 0 # for i in range(len(A)): # diff_dict[i] = {} # for j in range(i): # diff = A[i] - A[j] # if diff in diff_dict[j]: # diff_dict[i][diff] = diff_dict[j][diff] + 1 # else: # diff_dict[i][diff] = 1 # max_ = max(max_, diff_dict[i][diff]) # return max_ # diff_dict = {} # max_ = 0 # for i in range(len(A)): # diff_dict[i] = {} # for j in range(i): # curr = A[i] # prev = A[j] # diff = curr - prev # if diff in diff_dict[j]: # diff_dict[i][diff] = diff_dict[j][diff] + 1 # else: # diff_dict[i][diff] = 1 # max_ = max(max_, diff_dict[i][diff]) # return (max_ + 1) # diff_dict = {} # max_ = 0 # for i in range(len(A)): # diff_dict[i] = {} # for j in range(i): # curr = A[i] # prev = A[j] # diff = curr - prev # if diff in diff_dict[j]: # diff_dict[i][diff] = diff_dict[j][diff] + 1 # else: # diff_dict[i][diff] = 1 # max_ = max(max_, diff_dict[i][diff]) # print(diff_dict) # return (max_ + 1) # mapping = {} # if len(A) < 2: # return len(A) # max_ = 0 # for i in range(len(A)): # mapping[i] = {} # for j in range(0, i): # diff = A[i] - A[j] # if diff not in mapping[j]: # mapping[i][diff] = 2 # else: # mapping[i][diff] = mapping[j][diff] + 1 # max_ = max(max_, mapping[i][diff]) # return max_ # track = [0 for i in range(len(A))] # for i in range(1, len(A)): # track[i] = A[i] - A[i-1] # print(track) # num = {} # for i in range(1, len(track)): # val = (track[i] - track[i-1]) # if val in num: # num[val] += 1 # else: # num[val] = 1 # print(num) # return max(num.values()) + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) sol = 1 l_d = [{} for _ in range(n)] for j in range(1,n): dj = l_d[j] for i in range(j): diff = A[j] - A[i] di = l_d[i] dj[diff] = max(dj.get(diff, 2), di.get(diff,1)+1) sol = max(sol, dj[diff]) return sol
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): dp[j, A[j]-A[i]] = dp.get((i, A[j] - A[i]), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # want subsequence that is longest arithmetic # dictionary with len longest subsequence at j # for i in range(A): # for j in range(i): # diff = j - i # difference between the two # dictionary[(diff, i)] = max(dictionary[(diff, i)], dictionary[(diff, j)]+1) # # (-5, 1) = 1 # (-2, 2) = 1 # (3, 2) = 1 # (-7,3) = 1 # (-2,3) = 1 long_len_sub_at_pos = {} for i in range(len(A)): for j in range(i): diff = A[i] - A[j] # if (diff, j) in long_len_sub_at_pos: # long_len_sub_at_pos[(diff, i)] = max(long_len_sub_at_pos[(diff, i)], long_len_sub_at_pos[(diff, j)] + 1) # else: # long_len_sub_at_pos[(diff, i)] = 2 sub_len_at_j = long_len_sub_at_pos.get((diff,j), 1) long_len_sub_at_pos[(diff, i)] = max(long_len_sub_at_pos.get((diff,i), 0), sub_len_at_j + 1) #values in the dictionary would be the length of hte subseq #loop over and find the max subseq return max(long_len_sub_at_pos.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A): dp = dict() for endi, endv in enumerate(A[1:], start = 1): for starti, startv in enumerate(A[:endi]): diff = endv - startv if (starti, diff) in dp: dp[(endi, diff)] = dp[(starti, diff)] + 1 else: dp[(endi, diff)] = 2 return max(dp.values()) class Solution: def longestArithSeqLength(self, A): dp = dict() for starti, startv in enumerate(A): for endi, endv in enumerate(A[starti+1:], start = starti+1): diff = endv - startv if (starti, diff) in dp: dp[(endi, diff)] = dp[(starti, diff)] + 1 else: dp[(endi, diff)] = 2 return max(dp.values()) class Solution: def longestArithSeqLength(self, A): N = len(A) dp = [{0:1} for _ in range(N)] for end in range(1, N): for start in range(end): diff = A[end] - A[start] if diff in dp[start]: dp[end][diff] = dp[start][diff] + 1 else: dp[end][diff] = 2 return max(max(dp[end].values()) for end in range(1, N)) class Solution: def longestArithSeqLength(self, A): ans = 2 n = len(A) index = {} dp = [[2] * n for i in range(n)] for i, val in enumerate(A[:-1]): for j in range(i+1, n): first = val + val - A[j] if first in index: dp[i][j] = dp[index[first]][i] + 1 #ans = max(ans, dp[i][j]) if dp[i][j] > ans: ans = dp[i][j] index[val] = i return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: seen = set() for i in range(len(A)): for j in range(i,len(A)): seen.add(A[j] - A[i]) ans = 0 for j in seen: d = defaultdict(int) for i in A: d[i] = d[i-j] + 1 ans = max(ans,d[i]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} n = len(A) ans = 0 for i in range(n - 1): for j in range(i + 1, n): d = A[j] - A[i] dp[j, d] = dp.get((i, d), 1) + 1 ans = max(ans, dp[j, d]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: n = len(A) if n < 3: return n self.memo = {} self.res = 0 for i in range(n - 1): for j in range(i + 1, n): diff = A[j] - A[i] if (i, diff) in self.memo: self.memo[(j, diff)] = self.memo[(i, diff)] + 1 else: self.memo[(j, diff)] = 2 self.res = max(self.res, self.memo[(j, diff)]) return self.res
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} ans = 1 for i in range(len(A)): for j in range(i + 1, len(A)): dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1 ans = max(ans, dp[(j, A[j] - A[i])]) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: from collections import Counter, defaultdict min_val = min(A) max_val = max(A) global_best = -1 dp = {} prev = defaultdict(list) for i, v in enumerate(A): dp[i] = {} # print(f'--------- PROCESSING INDEX {i}') for d in range(min_val-v, max_val-v+1): # print(f'PROCESSING DIFF {d}') if v+d < min_val or v+d > max_val: raise Exception() best = 0 if v+d in prev: for j in prev[v+d]: best = max(best, dp[j].get(d, 1)) dp[i][d] = best + 1 global_best = max(global_best, dp[i][d]) prev[v].append(i) # print(dp) return global_best
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: lim = len(A) d = dict() ans = [] for i in range(0,lim): d[i]=dict() ans.append(2) for i in range(1,lim): for j in range(0,i): if A[i]-A[j] in d[j]: d[i][A[i]-A[j]]=d[j][A[i]-A[j]]+1 if d[i][A[i]-A[j]]>ans[i]:ans[i] = d[i][A[i]-A[j]] else: d[i][A[i]-A[j]] = 2 ###print(d) return max(ans)
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: # dp approach dp = {} for i in range(len(A)): for j in range(i + 1, len(A)): dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: largestLen = 0 longestArithSequence = [defaultdict(lambda: 1) for _ in range(len(A))] for i in range(1, len(A)): for j in range(i): diff = A[i] - A[j] seqLen = longestArithSequence[i][diff] = max(longestArithSequence[i][diff], longestArithSequence[j][diff] + 1) largestLen = max(largestLen, seqLen) return largestLen
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = {} N = len(A) for i in range(N): for j in range(i+1, N): dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1)+1 return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
from collections import defaultdict class Solution: def longestArithSeqLength(self, A: List[int]) -> int: d = {} mval = -1 for i, n in enumerate(A): for j in range(i): if (j, n-A[j]) not in d: d[(i, n-A[j])] = 2 else: d[(i, n-A[j])] = d[(j, n-A[j])] + 1 mval = max(mval, d[(i, n-A[j])]) return mval
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: res = 0 dp = collections.defaultdict(lambda: 1) for i in range(len(A)-1, -1, -1): for j in range(i+1, len(A)): dist = A[j] - A[i] dp[(i, dist)] = max(dp[(i, dist)], dp[(j, dist)] + 1) return max(dp.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: table = [collections.defaultdict(int) for _ in range(len(A))] ans = 0 for i in range(len(A)): for j in range(i): if A[i] - A[j] in table[j]: currLen = table[j][A[i] - A[j]] + 1 else: currLen = 2 ans = max(ans, currLen) table[i][A[i]-A[j]] = max(table[i][A[i]-A[j]], currLen) # print(table) return ans
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: if len(A) < 3: return len(A) sub_lens = [{} for i in A] max_len = 0 for i in range(0, len(A)): for j in range(0, i): diff = A[i] - A[j] sub_lens[i][diff] = max(sub_lens[i].get(diff, 1), sub_lens[j].get(diff, 1) + 1) max_len = max(max_len, sub_lens[i][diff]) return max_len
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = collections.defaultdict(int) for i in range(len(A)): for j in range(i): dp[(i, A[i] - A[j])] = dp[(j, A[i] - A[j])] + 1 return max(dp.values())+1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: memo = {} for i in range(len(A)): for j in range(i+1, len(A)): if (i, A[j] - A[i]) in memo: memo[j, A[j] - A[i]] = memo[i, A[j]-A[i]] + 1 else: memo[j, A[j] - A[i]] = 2 return max(memo.values())
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: dp = collections.defaultdict(lambda: collections.defaultdict(int)) maxSoFar = 0 for curr in range(len(A)): for prev in range(curr): difference = A[curr] - A[prev] dp[curr][difference] = max(dp[curr][difference], dp[prev][difference] + 1) maxSoFar = max(maxSoFar, dp[curr][difference]) return maxSoFar + 1
Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).   Example 1: Input: A = [3,6,9,12] Output: 4 Explanation: The whole array is an arithmetic sequence with steps of length = 3. Example 2: Input: A = [9,4,7,2,10] Output: 3 Explanation: The longest arithmetic subsequence is [4,7,10]. Example 3: Input: A = [20,1,15,3,10,5,8] Output: 4 Explanation: The longest arithmetic subsequence is [20,15,10,5].   Constraints: 2 <= A.length <= 1000 0 <= A[i] <= 500
class Solution: def longestArithSeqLength(self, A: List[int]) -> int: mem = [collections.defaultdict(int) for _ in A] res = 1 for i in range(len(A)): for j in range(i+1, len(A)): v = A[j] - A[i] mem[j][v]=max(mem[i][v] + 1, mem[j][v]) res = max(res, mem[j][v]) return res + 1