Skip to content

Commit 5830946

Browse files
authored
Create longest-arithmetic-subsequence-of-given-difference.py
1 parent fcb46fe commit 5830946

File tree

1 file changed

+19
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)