diff options
author | Jack Sangdahl <[email protected]> | 2025-02-26 19:55:41 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-02-26 19:55:41 -0700 |
commit | d9f08bf1f55bbdd365c8204ad5261a66f7d8fc25 (patch) | |
tree | 8cbc27d76255220209bcdf48aac77ed9d1305b23 /len_longest_fib_seq.py | |
parent | e8c16bee275788caf61d61fe641864fdb69447a9 (diff) | |
download | leetcode-d9f08bf1f55bbdd365c8204ad5261a66f7d8fc25.tar.gz leetcode-d9f08bf1f55bbdd365c8204ad5261a66f7d8fc25.zip |
873. length of longest fibonacci subsequence
Diffstat (limited to 'len_longest_fib_seq.py')
-rw-r--r-- | len_longest_fib_seq.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/len_longest_fib_seq.py b/len_longest_fib_seq.py new file mode 100644 index 0000000..619a3db --- /dev/null +++ b/len_longest_fib_seq.py @@ -0,0 +1,32 @@ +# 873. Length of Longest Fibonacci Subsequence +from collections import defaultdict + +""" +a sequence 'x1, x2, ..., xn' is fibonacci like if 'n >= 3' and 'x_i + x_i + 1 +== x_i + 2' for all 'i + 2 <= n'. given a strictly increasing 'arr' of +positive integers forming a sequence, return the length of the longest +fibonacci like subsequence of 'arr'. if one does not exist, return 0. a +subsequence is derived from another sequence 'arr' by deleting any number of +elements (including none) from 'arr', without changing the order of the +remaining elements. +""" + + +class Solution(object): + def lenLongestFibSubseq(self, arr): + """ + :type arr: List[int] + :rtype: int + """ + dp, s = defaultdict(int), set(arr) + for i in range(len(arr)): + for j in range(i): + if arr[i] - arr[j] < arr[j] and arr[i] - arr[j] in s: + dp[arr[j], arr[i]] = dp.get((arr[i] - arr[j], arr[j]), 2) + 1 + return max(dp.values() or [0]) + + +if __name__ == "__main__": + obj = Solution() + print(obj.lenLongestFibSubseq(arr=[1, 2, 3, 4, 5, 6, 7, 8])) + print(obj.lenLongestFibSubseq(arr=[1, 3, 7, 11, 12, 14, 18])) |