Skip to content

Commit 7f56e03

Browse files
author
lucifer
committed
feat: $873
1 parent 0871ffd commit 7f56e03

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
347347
- [0801. 使序列递增的最小交换次数](./problems/801.minimum-swaps-to-make-sequences-increasing.md) 🆕
348348
- [0816. 模糊坐标](./problems/816.ambiguous-coordinates.md)
349349
- [0820. 单词的压缩编码](./problems/820.short-encoding-of-words.md)
350+
- [0873. 最长的斐波那契子序列的长度](./problems/873.length-of-longest-fibonacci-subsequence.md) 🆕
350351
- [0875. 爱吃香蕉的珂珂](./problems/875.koko-eating-bananas.md)
351352
- [0877. 石子游戏](./problems/877.stone-game.md)
352353
- [0886. 可能的二分法](./problems/886.possible-bipartition.md)

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
- [0801. 使序列递增的最小交换次数](./problems/801.minimum-swaps-to-make-sequences-increasing.md) 🆕
200200
- [0816. 模糊坐标](./problems/816.ambiguous-coordinates.md)
201201
- [0820. 单词的压缩编码](./problems/820.short-encoding-of-words.md)
202+
- [0873. 最长的斐波那契子序列的长度](./problems/873.length-of-longest-fibonacci-subsequence.md) 🆕
202203
- [0875. 爱吃香蕉的珂珂](./problems/875.koko-eating-bananas.md)
203204
- [0877. 石子游戏](./problems/877.stone-game.md)
204205
- [0886. 可能的二分法](./problems/886.possible-bipartition.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## 题目地址(873. 最长的斐波那契子序列的长度)
2+
3+
https://leetcode-cn.com/problems/length-of-longest-fibonacci-subsequence/
4+
5+
## 题目描述
6+
7+
```
8+
如果序列 X_1, X_2, ..., X_n 满足下列条件,就说它是 斐波那契式 的:
9+
10+
n >= 3
11+
对于所有 i + 2 <= n,都有 X_i + X_{i+1} = X_{i+2}
12+
13+
给定一个严格递增的正整数数组形成序列,找到 A 中最长的斐波那契式的子序列的长度。如果一个不存在,返回  0 。
14+
15+
(回想一下,子序列是从原序列 A 中派生出来的,它从 A 中删掉任意数量的元素(也可以不删),而不改变其余元素的顺序。例如, [3, 5, 8] 是 [3, 4, 5, 6, 7, 8] 的一个子序列)
16+
17+
 
18+
19+
示例 1:
20+
21+
输入: [1,2,3,4,5,6,7,8]
22+
输出: 5
23+
解释:
24+
最长的斐波那契式子序列为:[1,2,3,5,8] 。
25+
26+
27+
示例 2:
28+
29+
输入: [1,3,7,11,12,14,18]
30+
输出: 3
31+
解释:
32+
最长的斐波那契式子序列有:
33+
[1,11,12],[3,11,14] 以及 [7,11,18] 。
34+
35+
36+
 
37+
38+
提示:
39+
40+
3 <= A.length <= 1000
41+
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
42+
(对于以 Java,C,C++,以及 C# 的提交,时间限制被减少了 50%)
43+
```
44+
45+
## 前置知识
46+
47+
- 动态规划
48+
49+
## 公司
50+
51+
- 暂无
52+
53+
## 思路
54+
55+
和一般的 DP 不同,这道题是已知状态转移方程。所以我勉强也归类到 DP 吧。
56+
57+
这道题的思路是两两枚举数组中的数字,不妨称其为 a 和 b。接下来,我们以 a 和 b 为斐波那契的起点, 很明显斐波那契数列的下一个数字应该是 a + b,这是题目给出的信息。
58+
59+
- 如果 a + b 不在数组中,直接终止,继续枚举下一个。
60+
- 如果 a + b 在数组中,说明我们找到了一个长度为 3 的斐波那契子数列。那么继续尝试扩展斐波那契数列长度到 4。。。
61+
62+
上面的枚举需要 $O(n^2)$的时间复杂度,枚举过程记录最大长度并返回即可。
63+
64+
对于每次枚举,我们都需要不断重复检查 a + b 是否在数组中,直到不再数组中为止。因此最坏的情况是一直在数组中,这个时间复杂度大概是数组中最大值和最小值的差值的对数。用公式表示就是 $log(m1 - m2)$,其中 m1 为数组 最大值, m2 为数组最小值。
65+
66+
## 关键点
67+
68+
- 使用集合存储数组中的所有数,然后枚举数组中的两两组合并,去集合中不断延伸斐波那契数列
69+
70+
## 代码
71+
72+
- 语言支持:Python3
73+
74+
Python3 Code:
75+
76+
```python
77+
78+
class Solution:
79+
def lenLongestFibSubseq(self, A: List[int]) -> int:
80+
s = set(A)
81+
ans = 0
82+
for i in range(len(A)):
83+
for j in range(i + 1, len(A)):
84+
a, b = A[j], A[i] + A[j]
85+
t = 2
86+
while b in s:
87+
a, b = b, a + b
88+
t += 1
89+
ans = max(ans, t)
90+
return 0 if ans < 3 else ans
91+
92+
```
93+
94+
**复杂度分析**
95+
96+
令 n 为数组长度, m1 为数组最大值,m2 为数组最小值。
97+
98+
- 时间复杂度:$O(n^2log(m1-m2))$
99+
- 空间复杂度:$O(n)$
100+
101+
## 扩展
102+
103+
这道题还有时间复杂度更好的做法, 感兴趣的可以参考 [力扣官方题解](https://leetcode-cn.com/problems/length-of-longest-fibonacci-subsequence/solution/zui-chang-de-fei-bo-na-qi-zi-xu-lie-de-chang-du-by/)
104+
105+
## 结尾
106+
107+
> 此题解由 [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) 自动生成。
108+
109+
力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时间收到我的动态啦~
110+
111+
以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
112+
113+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
114+
115+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

0 commit comments

Comments
 (0)