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 = [collections.defaultdict(int) for _ in A]
res = 1
for i in range(0,len(A)):
for j in range(i):
v = A[i]-A[j]
d[i][v]=max(d[j][v]+1,d[i][v])
res = max(d[i][v],res)
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=[]
m=0
for i,a in enumerate(A):
dp.append(defaultdict(int))
for j in range(i):
dif=a-A[j]
dp[i][dif]=dp[j][dif]+1
m=max(m,dp[i][dif])
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
|
class Solution:
def longestArithSeqLength(self, A: List[int]) -> int:
N = len(A)
dp = [collections.defaultdict(int) for _ in A]
res = 1
for i in range(N):
for j in range(i):
diff = A[i] - A[j]
if dp[i][diff] == None: dp[i][diff] = 0
if dp[j][diff] == None: dp[j][diff] = 0
if dp[j][diff] == 0:
dp[i][diff] = 2
else:
dp[i][diff] = dp[j][diff] + 1
res = max(res, dp[i][diff])
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:
n = len(A)
# lengths of AM seqs after i with a diff d
mem = [defaultdict(int) for _ in range(n)]
res = 0
for i in reversed(range(n)):
for j in range(i + 1, n):
d = A[j] - A[i]
mem[i][d] = max(1 + mem[j].get(d, 0), mem[i][d])
res = max(mem[i][d], res)
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:
if len(A) < 2:
return 0
la = len(A)
dp = {}
curr = 0
for i in range(1, la):
for j in range(i):
d = A[i] - A[j]
dp[(i, d)] = dp.get((j, d), 1) + 1
if dp[(i,d)] > curr:
curr = dp[(i,d)]
return curr
|
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 = [collections.defaultdict(int) for _ in A]
res = 1
for i in range(0,len(A)):
for j in range(i):
v = A[i]-A[j]
d[i][v]=max(d[j][v]+1,d[i][v])
res = max(d[i][v],res)
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:
memo = {}
for j, y in enumerate(A):
for i, x in enumerate(A[:j]):
d = y - x
memo[d, j] = memo.setdefault((d, i), 1) + 1
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:
N = len(A)
dp = [collections.defaultdict(int) for _ in range(N)]
res = 0
for i in range(1, N):
for j in range(i):
delta = A[i] - A[j]
dp[i][delta] = max(dp[i][delta], dp[j][delta] + 1, 2)
res = max(res, dp[i][delta])
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):
d = [collections.defaultdict(int) for _ in A]
res = 1
for i in range(0,len(A)):
for j in range(i):
v = A[i]-A[j]
d[i][v]=max(d[j][v]+1,d[i][v])
res = max(d[i][v],res)
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 = {}
if len(A) == 0:
return 0
if len(A) == 1:
return 1
for i in range(1, len(A)):
for j in range(0, i):
diff = A[i] - A[j]
if (j, diff) in dp:
dp[(i, diff)] = 1 + dp[(j, diff)]
else:
dp[i, diff] = 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
|
from collections import defaultdict
class Solution:
# @param A : tuple of integers
# @return an integer
def longestArithSeqLength(self, A: List[int]) -> int:
sol = defaultdict(int)
n = len(A)
for i in range(n):
for j in range(i):
d = A[i] - A[j]
sol[(i, d)] = max(sol[(i, d)], 1 + sol[(j, d)])
return max(list(sol.values()), default=0) + 1
# rows = len(A)
# cols = len(A)
# dp = [[2 for c in range(cols)] for r in range(rows)]
# for c in range(cols):
# for r in range(0, c):
# for c in range(cols):
# for r in range(0, c):
# diff = A[c] - A[r]
# x = A[r] - diff
# # search for k such that dp[k][r] A[k]=x
# for k in reversed(range(0, r)): # put this in doc
# if(A[k] == x):
# dp[r][c] = max(dp[k][r] + 1, dp[r][c])
# break
# max_so_far = dp[0][0]
# for c in range(cols):
# for r in range(0, c):
# max_so_far = max(max_so_far, dp[r][c])
# return max_so_far
|
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(int)
for j in range(N):
for i in range(j):
t = A[j] - A[i]
dp[j, t] = max(dp[j, t], dp[i, t] + 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:
#这题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)] = max(dp[(i, diff)], 1+ dp[(j, 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:
def longestArithSeqLength(self, A: List[int]) -> int:
if not A: return 0
dp = [{} for _ in range(len(A))]
res = 0
for j in range(1, len(A)):
for i in range(j):
if A[j]-A[i] in dp[i]:
dp[j][A[j]-A[i]] = dp[i][A[j]-A[i]]+1
else:
dp[j][A[j]-A[i]] = 1
res = max(dp[j][A[j]-A[i]], res)
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:
if not A:
return 0
res = 0
records = [collections.defaultdict(int) for _ in range(len(A))]
for i in range(len(A)):
for j in range(i):
diff = A[i] - A[j]
current = records[i].get(diff, 0)
prev = records[j].get(diff, 0) + 1
records[i][diff] = max(prev, current, 2)
res = max(res, records[i][diff])
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:
n = len(A)
dp = [collections.defaultdict(lambda: 1) for _ in range(n)]
ans = 0
for i in range(1, n):
for j in range(0, i):
diff = A[i] - A[j]
dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)
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 = {}
# n = len(A)
# for i in range(1, n):
# 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())
d = [collections.defaultdict(int) for _ in A]
res = 1
n = len(A)
for i in range(n):
for j in range(i):
v = A[i]-A[j]
d[i][v]=max(d[j][v]+1,d[i][v])
res = max(d[i][v],res)
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 = 1
for x in range(len(A)):
for y in range(x + 1, len(A)):
dp[y, A[y] - A[x]] = dp.get((x, A[y] - A[x]), 1) + 1
ans = max(dp[y, A[y] - A[x]], 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:
length = len(A)
onright = [0 for _ in range(501)]
toextend = [{} for _ in range(501)]
res = 2
for v in A:
onright[v] += 1
for i in range(0, length):
val = A[i]
tex = toextend[val]
onright[val] -= 1
doneself = False
for lval in A[:i]:
diff = val - lval
nextval = val + diff
if nextval == val:
if doneself:
continue
doneself = True
if nextval > 500 or nextval < 0 or onright[nextval] == 0:
continue
if diff in tex:
c = tex[diff] + 1
else:
c = 3
if c > res:
res = c
toextend[nextval][diff] = c
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 len(A)<=2:
return len(A)
seen = {}
ans = 0
for i in range(len(A)-1):
for j in range(i+1,len(A)):
seen[j, A[j]-A[i]] = seen.get((i, A[j]-A[i]), 0) + 1
ans = max(ans, seen[j, A[j]-A[i]])
return ans+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 = [collections.defaultdict(lambda:0) for _ in A]
# res = 1
# for i in range(len(A)):
# for j in range(i):
# v = A[i]-A[j]
# d[i][v]=max(d[j][v]+1,d[i][v])
# res = max(d[i][v],res)
# return res+1
d = [collections.defaultdict(lambda:0) for _ in A]
res = 1
for i in range(len(A)):
for j in range(i+1,len(A)):
v = A[i]-A[j]
d[j][v]=max(d[i][v]+1,d[j][v])
res = max(d[j][v],res)
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:
ans = 0
dp = {}
for i in range(len(A)):
for j in range(i):
diff = A[i] - A[j]
j1 = (j , diff)
i1 = (i, diff)
if dp.get(j1) != None:
dp[i1] = 1 + dp[j1]
ans = max(ans, dp[i1])
else:
dp[i1] = 2
ans = max(ans, dp[i1])
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:
def helper():
return 1
dp = defaultdict(helper)
for i in range(len(A)):
for j in range(i):
step = A[i] - A[j]
dp[(i, step)] = max(dp[(j, step)] + 1, dp[(i, step)])
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 = collections.defaultdict(int)
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:
# longest length with end index i and diff j
if not A:
return 0
dp = {}
n = len(A)
max_len = 0
for j in range(1, n):
for i in range(0, j):
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
max_len = max(max_len, dp[(j, A[j] - A[i])])
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):
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[(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
|
class Solution:
def longestArithSeqLength(self, A: List[int]) -> int:
dp = {}
for i, v in enumerate(A):
for j in range(i):
diff = v - A[j]
dp.setdefault((diff, i), 1)
dp[diff, i] = max(dp[diff, i], dp.get((diff, j), 1) + 1)
return max(dp.values(), default=0)
|
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))]
max_len = 0
for i in range(len(A)-1):
for j in range(i+1, len(A)):
diff = A[j] - A[i]
if dp[i].get(diff) == None:
dp[j][diff] = 2
else:
dp[j][diff] = dp[i][diff] + 1
max_len = max(dp[j][diff], max_len)
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 = []
maximum = 1
for i in range(len(A)):
d={}
dp.append(d)
for j in range(i):
dp[i][A[i]-A[j]] = dp[j][A[i]-A[j]] = 0
for i in range(len(A)):
d = {}
dp.append({})
for j in range(i):
diff = A[i]-A[j]
dp[i][diff] = dp[j][diff]+1
maximum = max(maximum,dp[i][diff])
return maximum+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:
n = len(A)
dp = [defaultdict(lambda: 1) for _ in range(n)]
ans = 2
for i in range(n):
for j in range(i):
dp[i][A[i]-A[j]]=max(dp[j][A[i]-A[j]]+1,2)
ans = max(ans,dp[i][A[i]-A[j]])
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: 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)
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: List[int]) -> int:
from collections import defaultdict
opt = defaultdict(lambda : defaultdict(int))
l = len(A)
sol = 0
# print(opt[-4])
for i in range(l): # 6
# print(opt[-17])
for j in range(i + 1, l): # 8
diff = A[j] - A[i] # 0
sub_l = 2
if diff in opt[i]:
sub_l = opt[i][diff] + 1
opt[j][diff] = max(opt[j][diff], sub_l)
sol = max(sol, opt[j][diff])
# if opt[A[i]][diff] == 7:
# print(i, A[i], diff)
# for i, row in enumerate(opt):
# print(i, row)
# print(dict(opt))
# print(opt[-4])
# for k, v in opt.items():
# print(k, v)
# if k < 0:
# print(k, v)
# pass
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:
# dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k
largest = max(A)
smallest = min(A)
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
if (i, step) not in dp:
dp[(i, step)] = prev_step + 1
else:
dp[(i, step)] = max(prev_step + 1, dp[(i, step)])
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
|
class Solution:
def longestArithSeqLength(self, A: List[int]) -> int:
from collections import Counter
cnt = Counter()
cnt
arith = [Counter() for i in range(len(A))]
for i in range(len(A)-2, -1,-1):
for j in range(i+1, len(A)):
diff = A[j]-A[i]
arith[i][diff] = max(1 + arith[j][diff], arith[i][diff])
#print(arith)
longest = 0
for i in range(len(A)):
#print(arith[i])
most_common = arith[i].most_common()
longest = max(most_common[0][1] if most_common else 0, longest)
return longest + 1
# for i in range(len(A)):
# for j in range(i+1, len(A)):
# cnt[A[j]-A[i]] += 1
# print(A[i], cnt)
# print(cnt)
# val = cnt.most_common()[0][1]
# return val + 1
# self.arith = [dict() for i in range(len(A))]
# def helper(i, diff):
# if diff in self.arith[i]:
# return self.arith[i][diff]
# val = 0
# for j in range(i+1, len(A)):
# if A[j] - A[i] == diff:
# val = 1 + helper(j, diff)
# break
# self.arith[i][diff] = val
# return self.arith[i][diff]
|
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 = 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] = max(dp[dif][i], dp[dif].get(j,0) + 1)
max_val = max(dp[dif][i], max_val)
#print(dp)
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):
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 in range(n-1):
for j in range(i+1, n):
first = A[i] * 2 - 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[A[i]] = 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:
#{#next num: cur length}
dp = [{} for a in A]
imax = 1
for i in range(len(A)):
for j in range(i-1,-1,-1):
diff = A[i]-A[j]
prev = dp[i].get(diff, 1)
dp[i][diff] = max(dp[j].get(diff, 1)+1, prev)
imax = max(imax, dp[i][diff])
return imax
|
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) <= 1:
return len(A)
# memo = [(1 + 2 * 500) * [1] for _ in range(1 + len(A))]
# res = 0
# for i in range(len(A)-2, -1, -1):
# for j in range(i+1, len(A)):
# diff = A[j] - A[i] + 500
# memo[i][diff] = max(memo[i][diff], memo[j][diff] + 1)
# res = max(res, memo[i][diff])
h = dict()
res = 0
for i in range(1, len(A)):
for j in range(i):
diff = A[j] - A[i]
h.setdefault((j, diff), 1)
h[(i, diff)] = h[(j, diff)] + 1
res = max(res, h[(i, diff)])
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:
dp=dict()
mlen=0
for i in range(len(A)):
dp[A[i]]=defaultdict(lambda :1)
for j in range (i-1,-1,-1):
d=A[i]-A[j]
if dp[A[i]][d]<=dp[A[j]][d]+1:
dp[A[i]][d]=dp[A[j]][d]+1
mlen=max(mlen,dp[A[i]][d])
return mlen
|
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:
#diffs = set(y - x for i, x in enumerate(A) for y in A[i+1:])
ans = 0
for diff in range(-500, 501):
data = {}
for num in A:
if num - diff not in data:
if num not in data:
data[num] = [num]
continue
if len(data[num - diff]) < len(data.get(num, [])):
continue
seq = data.pop(num - diff)
seq.append(num)
ans = max(ans, len(seq))
data[num] = seq
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
|
import itertools
class Solution:
def longestArithSeqLength(self, A: List[int]) -> int:
d = {}
for i in range(len(A)):
for j in range(i+1,len(A)):
check = (i,A[j]-A[i])
if check in d:
d[(j,A[j]-A[i])] = d[check]+[A[j]]
else:
d[(j,A[j]-A[i])] = [A[i],A[j]]
return len(max([i for i in list(d.values())],key=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 = [{} for i in range(len(A))]
res = 0
for i in range(1, len(A)):
for j in range(i):
dp[i][A[i]-A[j]] = max(dp[i].get(A[i]-A[j], 0), dp[j].get(A[i]-A[j], 0) + 1)
res = max(res, dp[i][A[i]-A[j]])
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:
n = len(A)
dp = [{} for _ in range(n)]
ans = 2
for i in range(1, n):
for j in range(i):
key_i = (A[i], A[i] - A[j])
key_j = (A[j], A[i] - A[j])
if key_i not in dp[i]:
dp[i][key_i] = 2
if key_j in dp[j]:
dp[i][key_i] = max(dp[i][key_i], dp[j][key_j] + 1)
ans = max(dp[i][key_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:
M=max(A)+1
dp=[]
for i in range(len(A)):
temp=[1]*(M*2)
dp.append(temp)
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=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:
res = 2
n = len(A)
dp = [collections.defaultdict(int) for _ in range(n)]
for i in range(1, n):
for j in range(i):
d = A[j] - A[i]
dp[i][d] = dp[j][d] + 1
res = max(res, dp[i][d]+1)
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:
seen = {}
res = 0
for i in range(len(A)):
for j in range(i-1, -1, -1):
diff = A[i] - A[j]
if diff not in seen:
seen[diff] = {}
old_val = seen[diff][i] if i in seen[diff] else 0
if j not in seen[diff]:
seen[diff][i] = 2
else:
seen[diff][i] = seen[diff][j] + 1
seen[diff][i] = max(old_val, seen[diff][i])
if seen[diff][i] > res:
res = seen[diff][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
|
from collections import defaultdict
class Solution:
def find(self, A):
dp = []
ans = 1
for _ in A: dp.append({})
for i, ai in enumerate(A):
for j in range(i):
aj = A[j]
d = ai - aj
if d < 0: continue
if d not in dp[i]: dp[i][d] = 1
if d not in dp[j]: dp[j][d] = 1
temp = max(dp[i][d], dp[j][d] + 1)
dp[i][d] = temp
ans = max(ans, temp)
return ans
def longestArithSeqLength(self, A: List[int]) -> int:
ans = self.find(A)
ans = max(ans, self.find(A[::-1]))
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:
ans = 0
#A = sorted(A)
for diff in range(-500,501):
dp = defaultdict(int)
for e in A:
dp[e] = dp[e-diff]+1
ans = max(ans,dp[e])
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 = {}
# res = -1
# for i in range(0, len(A)):
# for j in range(i-1, -1, -1):
# d = A[i] - A[j]
# dp[(i, d)] = max(dp.get((i, d), -1), dp.get((j, d), 1) + 1)
# res = max(res, dp[(i, d)])
# return res
dp = [[-1 for i in range(1001)] for j in range(len(A))]
res = -1
for i in range(0, len(A)):
for j in range(i-1, -1, -1):
d = A[i] - A[j] + 500
if dp[j][d] == -1: dp[j][d] = 1
dp[i][d] = max(dp[i][d], dp[j][d] + 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 = {}
hm = {A[0]:0}
for i in range(1, len(A)):
if A[i] not in hm:
hm[A[i]] = i
for j in range(i):
diff = A[i]-A[j]
# print(i, j, diff, dp, hm)
if (A[j]-diff) in hm:
dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1)
else:
dp[(i, diff)] = max(dp.get((i, diff), 2), 2)
# 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:
_max, _min = 0, 0
for ele in A:
_max = max(_max, ele)
_min = max(_min, ele)
diff = _min-_max
dp = [ {} for i in range(len(A))]
res = 0
for i in range(1, len(A)):
for j in range(i):
dp[i][A[i]-A[j]] = max(dp[i].get(A[i]-A[j], 0), dp[j].get(A[i]-A[j], 0) + 1)
res = max(res, dp[i][A[i]-A[j]])
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:
f = collections.defaultdict(int)
fff = collections.defaultdict(int)
maxlen = 0
for i in range(len(A)):
for j in range(i):
diff = A[i] - A[j]
#fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)
f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)
'''
if (j, diff) not in f:
f[(i, diff)] = 2
else:
f[(i, diff)] = max(f[(i, diff)], f[(j, diff)] + 1)
'''
maxlen = max(maxlen, f[(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 = {}
globalMax = 1
for i, a1 in enumerate(A):
for j, a2 in enumerate(A[:i]):
x = a1 - a2
if (j,x) in dp:
dp[(i,x)] = dp[(j,x)] + 1
else:
dp[(i,x)] = 2
globalMax = max(globalMax, dp[(i,x)])
return globalMax
|
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)
mx = 0
for i, n in enumerate(A):
for j in range(i+1, N):
b = A[j] - n
if (i, b) in dp:
dp[j, b] = dp[i, b] + 1
else:
dp[j, b] = 2
mx = max(mx, dp[j, b])
return mx
|
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 = {i:collections.defaultdict(lambda: 1) for i in range(len(A))}
for i in range(1, len(A)):
for j in range(i):
diff = A[i]-A[j]
dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)
res = max(res, dp[i][diff])
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[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:
def calc(A):
memo = [dict() for _ in range(len(A))]
ret = 1
for i in range(len(A) - 1, -1, -1):
for j in range(i + 1, len(A)):
if A[j] < A[i]:
continue
diff = A[j] - A[i]
memo[i][diff] = max(memo[i].get(diff, 0), memo[j].get(diff, 1) + 1)
ret = max(ret, memo[i][diff])
return ret
return max(
calc(A), calc(list(reversed(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:
res = 0
dp = [collections.defaultdict(lambda: 1) for _ in range(len(A))]
for i in range(len(A)):
for k in range(i-1, -1, -1):
dp[i][A[i] - A[k]] = max(dp[i][A[i]-A[k]], dp[k][A[i]-A[k]] + 1) # remember the MAX here!!!
res = max(res, dp[i][A[i]-A[k]])
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 = {}
length = len(A)
for i in range(length):
for j in range(i + 1, length):
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:
res = 0
dp = [collections.defaultdict(lambda: 1) for _ in range(len(A))]
for i in range(len(A)):
for k in range(i-1, -1, -1):
dp[i][A[i] - A[k]] = max(dp[i][A[i]-A[k]], dp[k][A[i]-A[k]] + 1)
res = max(res, dp[i][A[i]-A[k]])
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 len(A) < 2:
return 0
la = len(A)
dp = {}
curr = 0
for i in range(1, la):
for j in range(i):
d = A[i] - A[j]
dp[(i, d)] = dp.get((j, d), 1) + 1
curr = max(curr, dp.get((i,d), 1))
return curr
|
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 == 2:
return n
dp = [{} for i in range(n)]
max_len = 0
for i in range(1,n):
for j in range(i):
diff = A[i]-A[j]
if diff in dp[j]:
dp[i][diff] = max(2, 1+dp[j][diff])
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:
n = len(A)
table = [ defaultdict(int) for i in range(n) ]
out = 0
curr = A[1]-A[0]
full = True
for i in range(2,n):
if curr != A[i]-A[i-1]:
full = False
break
if full: return n
# print(table)f
for i in range(n):
for j in range(0,i):
diff = A[i]-A[j]
if table[j][diff] == 0:
table[j][diff] = 1
table[i][diff] = max(table[i][diff],table[j][diff] + 1)
out = max(table[i][diff], out)
# for i in range(n):
# print(A[i], table[i])
return out
|
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(lambda:1)
n = len(A)
for i in range(n):
for j in range(i):
dp[i, A[i] - A[j]] = max(dp[i, A[i] - A[j]], dp[j, A[i] - A[j]] + 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 = [collections.Counter() for _ in A]
res = 1
for i in range(0,len(A)):
for j in range(i):
v = A[i]-A[j]
d[i][v]=max(d[j][v]+1,d[i][v])
res = max(d[i][v],res)
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:
d = {}
mx = 0
for i in range(len(A)):
for j in range(i):
diff = A[i] - A[j]
if (diff, i) not in d:
d[(diff, i)] = 2
if (diff, j) in d:
d[(diff, i)] = max(d[(diff,i)], d[(diff, j)] + 1)
mx = max(mx, d[(diff, i)])
return mx
|
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(dict)
ans = 0
for i in range(len(A)):
for j in range(i):
diff = A[i] - A[j]
dp[i][diff] = dp[i].get(diff, 0)
dp[i][diff] = max(dp[i][diff], dp[j].get(diff,0)+1)
# if dp[i][diff]==6: print(A[i],A[j], diff)
ans = max(ans, dp[i][diff])
return ans+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:
if len(A) < 3:
return len(A)
n = len(A)
diff_c = collections.Counter()
for i in range(n):
for j in range(i):
if diff_c[(j, A[i]-A[j])] == 0:
diff_c[(i, A[i]-A[j])] = 2
else:
diff_c[(i, A[i] - A[j])] = diff_c[(j, A[i]-A[j])] + 1
return max(diff_c.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:
#{#next num: cur length}
dp = {}
imax = 1
for i in range(len(A)):
for j in range(i-1,-1,-1):
diff = A[i]-A[j]
prev = dp.get((diff, i), 1)
saved = dp[(diff, i)] = max(dp.get((diff, j),1)+1, prev)
imax = max(imax, saved)
return imax
|
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
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)
sol = max(sol, dp[j,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:
pos = defaultdict(int)
best = 1
for i in range(len(A) - 1, -1, -1):
for j in range(i + 1, len(A)):
diff = A[j] - A[i]
if (j, diff) in pos:
pos[(i, diff)] = max(pos[(i, diff)], pos[(j, diff)] + 1)
else:
pos[(i, diff)] = max(pos[(i, diff)], 2)
best = max(best, pos[(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:
minimum = min(A)
maximum = max(A)
length = 2*(maximum-minimum)+1
dp = [[1 for i in range(length)] for j in range(len(A))]
diff = maximum-minimum
ans = 0
for i in range(len(dp)):
for j in range(i):
dp[i][A[i]-A[j]+diff]=max(dp[i][A[i]-A[j]+diff],dp[j][A[i]-A[j]+diff]+1)
ans = max(ans,dp[i][A[i]-A[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:
if len(A)==0:
return 0
maxx=1
arSubCounts = dict()
for i in range(len(A)):
for j in range(i+1,len(A)):
diff = A[j]-A[i]
arSubCounts[(j,diff)]=max(arSubCounts.get((j,diff),1),arSubCounts.get((i,diff),1)+1)
maxx = max(arSubCounts[(j,diff)],maxx)
return maxx
|
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
seen = {}
longest = 1
for i in range(len(A)):
for j in range(i+1, len(A)):
diff = A[j]-A[i]
if (i, diff) not in seen:
seen[(i, diff)] = 1
if (j, diff) not in seen:
seen[(j,diff)] = 1
seen[(j,diff)] = max(seen[(j,diff)], seen[(i,diff)]+1)
return max([v for k,v in seen.items()])
|
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
|
import collections
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)
d = {}
res = 0
for i in range(n):
d[i] = {}
for j in range(i):
diff = A[i] - A[j]
if diff not in d[j]:
d[i][diff] = 2
else:
d[i][diff] = d[j][diff] + 1
res = max(res, d[i][diff])
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)
ans = 0
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)
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 = {}
ans = 0
for i in range(len(A)):
for j in range(i+1, len(A)):
diff = A[j] - A[i]
if (i, diff) not in dp:
dp[(j, diff)] = 2
else:
dp[(j, diff)] = dp[(i, diff)] + 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:
# if there's no A,
if not A:
return 0
# create a dp array,
# key would be a tuple of indices,
dp = {}
# iterate over the length of A,
for it1 in range (1, len (A)):
# iterate upto length of it1,
for it2 in range (it1):
# create a tuple by difference,
key_tuple = (it2, A[it1] - A[it2])
# if key_tuple doesn't exist,
if key_tuple not in dp:
dp[(key_tuple)] = 1
dp [(it1, A[it1] - A[it2])] = dp[(key_tuple)] + 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 = [collections.defaultdict(int) for _ in range(len(A))]
res = 0
for i in range(1, len(A)):
for j in range(i):
dp[i][A[i]-A[j]] = max(dp[i][A[i]-A[j]], dp[j].get(A[i]-A[j], 1)+1)
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
|
'''
[9,4,7,2,10]
0,
[3,6,9,12]
0 -> 1
3 -> 2
3 -> 3
6 -> 1
'''
class Solution:
def longestArithSeqLength(self, A: List[int]) -> int:
# if there's no list
if not A:
return 0
if len(A) == 1:
return 1
diff_map = {}
# iterate over indexes
for it1 in range (1, len(A)):
num1 = A[it1]
for it2 in range (it1):
num2 = A[it2]
# check the difference
diff = num1 - num2
if (it2, diff) not in diff_map:
diff_map[(it2, diff)] = 1
diff_map[(it1, diff)] = diff_map[(it2, diff)] + 1
#print (diff_map)
# return the maximum of values
return max (diff_map.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(i+1,len(A)):
dp[j,A[j]-A[i]] = dp.get((i,A[j]-A[i]),1)+1
res = max(res,dp[(j,A[j]-A[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:
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:
# -5:1
# -2:1 3:1 -5:1
# -7:1 -2:2 3:1 -5:2
#
if not A:
return 0
result = 0
N = len(A)
d= {}
for i in range(1,N):
for j in range(0,i):
diff = A[i] - A[j]
d[i,diff] = d.get((j,diff), 0 )+ 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):
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 in range(n-1):
for j in range(i+1, n):
first = A[i] + A[i] - 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[A[i]] = 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:
# this is very tricky, A inside can have duplicates
mem = {}
prev_values = set()
for idx, num in enumerate(A):
for pval in prev_values:
diff = num - pval
mem[num, diff] = max(mem.get((num, diff), 1), 1 + mem.get((pval, diff), 1))
prev_values.add(num)
return max(mem.values(), default = 0)
|
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)
sequenceEnds = dict()
maxSequenceLen = 2
for i in range(len(A)):
seenDiffs = set()
for j in range(i):
diff = A[i] - A[j]
if diff in seenDiffs:
continue
elif diff in sequenceEnds:
sequencesWithDiff = sequenceEnds[diff]
if A[j] in sequencesWithDiff:
sequenceLength = sequencesWithDiff[A[j]]
sequencesWithDiff[A[i]] = sequenceLength + 1
maxSequenceLen = max(maxSequenceLen, sequenceLength + 1)
else:
sequencesWithDiff[A[i]] = 2
else:
sequencesWithDiff = dict()
sequencesWithDiff[A[i]] = 2
sequenceEnds[diff] = sequencesWithDiff
seenDiffs.add(diff)
return maxSequenceLen
|
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 = 2
n = len(A)
index = {}
dp = [[2] * n for i in range(n)]
for i in range(n-1):
for j in range(i+1, n):
first = A[i] * 2 - A[j]
if first in index:
dp[i][j] = dp[index[first]][i] + 1
ans = max(ans, dp[i][j])
index[A[i]] = 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 = {}
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())
# dp = {}
# for i, a2 in enumerate(A[1:], start=1):
# for j, a1 in enumerate(A[:i]):
# d = a2 - a1
# 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:
# subsequence problem -> dp
# dp[i][j] -- length of arithmetic subsequence ending at ith and jth element
ans = 2
n = len(A)
index = {}
dp = [[2] * n for i in range(n)]
for i in range(n-1):
for j in range(i+1, n):
first = A[i] * 2 - A[j]
if first in index:
dp[i][j] = dp[index[first]][i] + 1
ans = max(ans, dp[i][j])
index[A[i]] = 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:
length = len(A)
onleft = set()
onleftl = []
onright = [0 for _ in range(501)]
toextend = [{} for _ in range(501)]
res = 2
for v in A:
onright[v] += 1
for i in range(0, length):
val = A[i]
tex = toextend[val]
onright[val] -= 1
for lval in onleftl:
diff = val - lval
nextval = val + diff
if nextval > 500 or nextval < 0 or onright[nextval] == 0:
continue
c = tex.get(diff, 2) + 1
if c > res:
res = c
else:
ending = (res - c) * diff + nextval
if ending > 500 or ending < 0 or onright[ending] == 0:
continue
toextend[nextval][diff] = c
if val not in onleft:
onleft.add(val)
onleftl.append(val)
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 in range(len(A))]
for i in range(len(A)):
dp[i]={0:1}
for j in range(i):
diff=A[i]-A[j]
if diff in dp[j]:
dp[i][diff]=dp[j][diff]+1
else:
dp[i][diff]=2
ans=0
for dic in dp:
if dic:
ans=max(ans,max(dic.values()))
#print(dp)
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 = [{} for _ in range(n)]
ans = 0
for i in range(n):
dp[i][0] = 1
for j in range(i):
diff = A[i] - A[j]
if diff not in dp[j]:
dp[i][diff] = 2
else:
dp[i][diff] = dp[j][diff] + 1
ans = max(ans, max(dp[i].values()))
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):
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 in range(n-1):
for j in range(i+1, n):
first = A[i] * 2 - A[j]
if first in index:
dp[i][j] = dp[index[first]][i] + 1
ans = max(ans, dp[i][j])
index[A[i]] = 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:
biggest = max(A)
def findLen(A: List[int]) -> int:
seen = set()
dp = defaultdict(lambda: defaultdict(lambda: 0))
for a in A:
for prev in seen:
gap = a - prev
newLen = 2 if dp[gap][prev] == 0 else 1 + dp[gap][prev]
dp[gap][a] = max(dp[gap][a], newLen)
seen.add(a)
return max([l for gaps in dp.values() for l in gaps.values()])
return findLen(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:
if not A:
return 0
n = len(A)
if n < 2:
return 0
res = 2
dp = [{} for i in range(n)]
dp[1] = {A[1]-A[0]: 2}
for k in range(2, n):
for i in range(k):
diff = A[k] - A[i]
if diff in dp[i]:
dp[k][diff] = dp[i][diff] + 1
else:
dp[k][diff] = 2
return max(max(item.values()) for item in dp if item)
|
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:
length = len(A)
onright = [0 for _ in range(501)]
toextend = [{} for _ in range(501)]
res = 2
for v in A:
onright[v] += 1
for i in range(0, length):
val = A[i]
tex = toextend[val]
onright[val] -= 1
doneself = False
for lval in A[:i]:
diff = val - lval
nextval = val + diff
if nextval > 500 or nextval < 0 or onright[nextval] == 0:
continue
if nextval == val:
if doneself:
continue
doneself = True
if diff in tex:
c = tex[diff] + 1
else:
c = 3
if c > res:
res = c
toextend[nextval][diff] = c
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:
from collections import defaultdict
d = [{} for _ in range(len(A))]
res = 2
for i, x in enumerate(A):
for j in range(i):
diff = x - A[j]
if diff in d[j]:
d[i][diff] = max(d[j][diff] + 1, d[i][diff]) if diff in d[i] else d[j][diff] + 1
d[j].pop(diff)
res = max(res, d[i][diff])
else:
d[i][diff] = 2
return res
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.