首页 > 其他分享 >LeetCode剑指 Offer II 093 最长斐波那契数列

LeetCode剑指 Offer II 093 最长斐波那契数列

时间:2022-09-30 17:26:28浏览次数:96  
标签:arr Offer 093 斐波 range LeetCode

LeetCode剑指 Offer II 093 最长斐波那契数列

class Solution:
    def lenLongestFibSubseq(self, arr: List[int]) -> int:

        n, loc, ans = len(arr), {}, 0
        for i in range(n): loc[arr[i]] = i
        f = [[0] * n for i in range(n)]

        for i in range(n):
            for j in range(i - 1, -1, -1):
                if arr[j] * 2 <= arr[i]: break
                else:
                    arr_k = arr[i] - arr[j]
                    if arr_k in loc:
                        k = loc[arr_k]
                        f[i][j] = max(f[j][k] + 1, 3)
                        ans = max(ans, f[i][j])
        
        return ans

标签:arr,Offer,093,斐波,range,LeetCode
From: https://www.cnblogs.com/solvit/p/16745565.html

相关文章