Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 151b2f2

Browse files
✨feat: add 873
1 parent 9d21d12 commit 151b2f2

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
| [736. Lisp 语法解析](https://leetcode.cn/problems/parse-lisp-expression/) | [LeetCode 题解链接](https://leetcode.cn/problems/parse-lisp-expression/solution/by-ac_oier-i7w1/) | 困难 | 🤩🤩🤩🤩 |
4747
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
4848
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
49+
| [873. 最长的斐波那契子序列的长度](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/) | [LeetCode 题解链接](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/solution/by-ac_oier-beo2/) | 中等 | 🤩🤩🤩 |
4950
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩🤩 |
5051
| [888. 公平的糖果棒交换](https://leetcode-cn.com/problems/fair-candy-swap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/fair-candy-swap/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-uant/) | 简单 | 🤩🤩 |
5152
| [890. 查找和替换模式](https://leetcode.cn/problems/find-and-replace-pattern/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-and-replace-pattern/solution/by-ac_oier-s4cw/) | 中等 | 🤩🤩🤩🤩 |

‎Index/序列 DP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
| [673. 最长递增子序列的个数](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/solution/gong-shui-san-xie-lis-de-fang-an-shu-wen-obuz/) | 中等 | 🤩🤩🤩🤩 |
1313
| [689. 三个无重叠子数组的最大和](https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/solution/gong-shui-san-xie-jie-he-qian-zhui-he-de-ancx/) | 困难 | 🤩🤩🤩🤩 |
1414
| [740. 删除并获得点数](https://leetcode-cn.com/problems/delete-and-earn/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/delete-and-earn/solution/gong-shui-san-xie-zhuan-huan-wei-xu-lie-6c9t0/) | 中等 | 🤩🤩🤩🤩🤩 |
15+
| [873. 最长的斐波那契子序列的长度](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/) | [LeetCode 题解链接](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/solution/by-ac_oier-beo2/) | 中等 | 🤩🤩🤩🤩🤩 |
1516
| [926. 将字符串翻转到单调递增](https://leetcode.cn/problems/flip-string-to-monotone-increasing/) | [LeetCode 题解链接](https://leetcode.cn/problems/flip-string-to-monotone-increasing/solution/by-ac_oier-h0we/) | 中等 | 🤩🤩🤩🤩🤩 |
1617
| [978. 最长湍流子数组](https://leetcode-cn.com/problems/longest-turbulent-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-turbulent-subarray/solution/xiang-jie-dong-tai-gui-hua-ru-he-cai-dp-3spgj/) | 中等 | 🤩🤩🤩 |
1718
| [1035. 不相交的线](https://leetcode-cn.com/problems/uncrossed-lines/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncrossed-lines/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-bkaas/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[873. 最长的斐波那契子序列的长度](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/solution/by-ac_oier-beo2/)** ,难度为 **中等**
4+
5+
Tag : 「序列 DP」、「哈希表」、「动态规划」
6+
7+
8+
9+
如果序列 $X_1, X_2, ..., X_n$ 满足下列条件,就说它是 斐波那契式 的:
10+
11+
* `n >= 3`
12+
* 对于所有 `i + 2 <= n`,都有 $X_i + X_{i+1} = X_{i+2}$
13+
14+
给定一个严格递增的正整数数组形成序列 `arr`,找到 `arr` 中最长的斐波那契式的子序列的长度。如果一个不存在,返回 0ドル$ 。
15+
16+
(回想一下,子序列是从原序列 `arr` 中派生出来的,它从 `arr` 中删掉任意数量的元素(也可以不删),而不改变其余元素的顺序。例如, `[3, 5, 8]``[3, 4, 5, 6, 7, 8]` 的一个子序列)
17+
18+
示例 1:
19+
```
20+
输入: arr = [1,2,3,4,5,6,7,8]
21+
22+
输出: 5
23+
24+
解释: 最长的斐波那契式子序列为 [1,2,3,5,8] 。
25+
```
26+
示例 2:
27+
```
28+
输入: arr = [1,3,7,11,12,14,18]
29+
30+
输出: 3
31+
32+
解释: 最长的斐波那契式子序列有 [1,11,12]、[3,11,14] 以及 [7,11,18] 。
33+
```
34+
35+
提示:
36+
* 3ドル <= arr.length <= 1000$
37+
* 1ドル <= arr[i] < arr[i + 1] <= 10^9$
38+
39+
---
40+
41+
### 序列 DP
42+
43+
定义 $f[i][j]$ 为使用 $arr[i]$ 为斐波那契数列的最后一位,使用 $arr[j]$ 为倒数第二位(即 $arr[i]$ 的前一位)时的最长数列长度。
44+
45+
不失一般性考虑 $f[i][j]$ 该如何计算,首先根据斐波那契数列的定义,我们可以直接算得 $arr[j]$ 前一位的值为 $arr[i] - arr[j],ドル而快速得知 $arr[i] - arr[j]$ 值的坐标 $t,ドル可以利用 `arr` 的严格单调递增性质,使用「哈希表」对坐标进行转存,若坐标 $t$ 存在,并且符合 $t < j,ドル说明此时至少凑成了长度为 3ドル$ 的斐波那契数列,同时结合状态定义,可以使用 $f[t][j]$ 来更新 $f[i][j],ドル即有状态转移方程:
46+
47+
$$
48+
f[i][j] = \max(3, f[j][t] + 1)
49+
$$
50+
51+
同时,当我们「从小到大」枚举 $i,ドル并且「从大到小」枚举 $j$ 时,我们可以进行如下的剪枝操作:
52+
53+
* 可行性剪枝:当出现 $arr[i] - arr[j] >= arr[j],ドル说明即使存在值为 $arr[i] - arr[j]$ 的下标 $t,ドル根据 `arr` 单调递增性质,也不满足 $t < j < i$ 的要求,且继续枚举更小的 $j,ドル仍然有 $arr[i] - arr[j] >= arr[j],ドル仍不合法,直接 `break` 掉当前枚举 $j$ 的搜索分支;
54+
* 最优性剪枝:假设当前最大长度为 `ans`,只有当 $j + 2 > ans,ドル我们才有必要往下搜索,$j + 2$ 的含义为以 $arr[j]$ 为斐波那契数列倒数第二个数时的理论最大长度。
55+
56+
代码:
57+
```Java
58+
class Solution {
59+
public int lenLongestFibSubseq(int[] arr) {
60+
int n = arr.length, ans = 0;
61+
Map<Integer, Integer> map = new HashMap<>();
62+
for (int i = 0; i < n; i++) map.put(arr[i], i);
63+
int[][] f = new int[n][n];
64+
for (int i = 0; i < n; i++) {
65+
for (int j = i - 1; j >= 0 && j + 2 > ans; j--) {
66+
if (arr[i] - arr[j] >= arr[j]) break;
67+
int t = map.getOrDefault(arr[i] - arr[j], -1);
68+
if (t == -1) continue;
69+
f[i][j] = Math.max(3, f[j][t] + 1);
70+
ans = Math.max(ans, f[i][j]);
71+
}
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
* 时间复杂度:存入哈希表复杂度为 $O(n)$;`DP` 过程复杂度为 $O(n^2)$。整体复杂度为 $O(n^2)$
78+
* 空间复杂度:$O(n^2)$
79+
80+
---
81+
82+
### 最后
83+
84+
这是我们「刷穿 LeetCode」系列文章的第 `No.873` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
85+
86+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
87+
88+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
89+
90+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
91+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /