Участник:Kiranov dmitry/task7

Материал из DISCOPAL
Перейти к: навигация, поиск

ссылка:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/

class Solution:
   def lenLongestFibSubseq(self, A: List[int]) -> int:
           n = len(A)
           res = 0
           F = [[0] * n for i in range(n)]
           di = {}
           for i in range(n):
               for j in range(i + 1, n):
                   if A[j] - A[i] in di:
                       F[i][j] = max(F[i][j], 1 + F[di[A[j] - A[i]]][i])
                       res = max(res, F[i][j])
               di[A[i]] = i
           return 0 if res == 0 else res+2