Skip to content

Commit 8b0f4c7

Browse files
authored
Create longest-arithmetic-sequence.py
1 parent 2bc712c commit 8b0f4c7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/longest-arithmetic-sequence.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(n^2)
2+
# Space: O(n^2)
3+
4+
import collections
5+
6+
class Solution(object):
7+
def longestArithSeqLength(self, A):
8+
"""
9+
:type A: List[int]
10+
:rtype: int
11+
"""
12+
dp = collections.defaultdict(int)
13+
for i in xrange(len(A)-1):
14+
for j in xrange(i+1, len(A)):
15+
v = A[j]-A[i]
16+
dp[v, j] = max(dp[v, j], dp[v, i]+1)
17+
return max(dp.values())+1

0 commit comments

Comments
 (0)