This commit is contained in:
parent
84a2d94754
commit
6be95b9fc0
1 changed files with 18 additions and 0 deletions
18
medium/length_of_longest_fibonacci_subsequence.py
Normal file
18
medium/length_of_longest_fibonacci_subsequence.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# https://leetcode.com/problems/length-of-longest-fibonacci-subsequence
|
||||||
|
|
||||||
|
from typing import Dict, List, Tuple
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def lenLongestFibSubseq(self, arr: List[int]) -> int:
|
||||||
|
indexes: Dict[int, int] = {num: i for i, num in enumerate(arr)}
|
||||||
|
d: Dict[Tuple[int, int], int] = {}
|
||||||
|
result = 0
|
||||||
|
|
||||||
|
for i in range(len(arr)):
|
||||||
|
for j in range(i):
|
||||||
|
k = indexes.get(arr[i] - arr[j])
|
||||||
|
if k is not None and k < j:
|
||||||
|
d[j, i] = d.get((k, j), 2) + 1
|
||||||
|
result = max(result, d[j, i])
|
||||||
|
return result
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue