题目描述
如果序列 X_1, X_2, ..., X_n
满足下列条件,就说它是 斐波那契式 的:
n >= 3
- 对于所有
i + 2 <= n
,都有 X_i + X_{i+1} = X_{i+2}
给定一个严格递增的正整数数组形成序列 arr
,找到 arr
中最长的斐波那契式的子序列的长度。如果一个不存在,返回 0 。
(回想一下,子序列是从原序列 arr
中派生出来的,它从 arr
中删掉任意数量的元素(也可以不删),而不改变其余元素的顺序。例如, [3, 5, 8]
是 [3, 4, 5, 6, 7, 8]
的一个子序列)
示例 1:
输入: arr = [1,2,3,4,5,6,7,8]
输出: 5
解释: 最长的斐波那契式子序列为 [1,2,3,5,8] 。
示例 2:
输入: arr = [1,3,7,11,12,14,18]
输出: 3
解释: 最长的斐波那契式子序列有 [1,11,12]、[3,11,14] 以及 [7,11,18] 。
提示:
注意:本题与主站 873 题相同: https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/
解法
方法一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | class Solution:
def lenLongestFibSubseq(self, arr: List[int]) -> int:
mp = {v: i for i, v in enumerate(arr)}
n = len(arr)
dp = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(i):
dp[j][i] = 2
ans = 0
for i in range(n):
for j in range(i):
delta = arr[i] - arr[j]
if delta in mp:
k = mp[delta]
if k < j:
dp[j][i] = dp[k][j] + 1
ans = max(ans, dp[j][i])
return ans
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 | class Solution {
public int lenLongestFibSubseq(int[] arr) {
int n = arr.length;
Map<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < n; ++i) {
mp.put(arr[i], i);
}
int[][] dp = new int[n][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
dp[j][i] = 2;
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int delta = arr[i] - arr[j];
if (mp.containsKey(delta)) {
int k = mp.get(delta);
if (k < j) {
dp[j][i] = dp[k][j] + 1;
ans = Math.max(ans, dp[j][i]);
}
}
}
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | class Solution {
public:
int lenLongestFibSubseq(vector<int>& arr) {
unordered_map<int, int> mp;
int n = arr.size();
for (int i = 0; i < n; ++i) mp[arr[i]] = i;
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j)
dp[j][i] = 2;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int delta = arr[i] - arr[j];
if (mp.count(delta)) {
int k = mp[delta];
if (k < j) {
dp[j][i] = dp[k][j] + 1;
ans = max(ans, dp[j][i]);
}
}
}
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 | func lenLongestFibSubseq(arr []int) int {
n := len(arr)
mp := make(map[int]int, n)
for i, v := range arr {
mp[v] = i + 1
}
dp := make([][]int, n)
for i := 0; i < n; i++ {
dp[i] = make([]int, n)
for j := 0; j < i; j++ {
dp[j][i] = 2
}
}
ans := 0
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
delta := arr[i] - arr[j]
k := mp[delta] - 1
if k >= 0 && k < j {
dp[j][i] = dp[k][j] + 1
ans = max(ans, dp[j][i])
}
}
}
return ans
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | class Solution {
func lenLongestFibSubseq(_ arr: [Int]) -> Int {
let n = arr.count
var mp = [Int: Int]()
for i in 0..<n {
mp[arr[i]] = i
}
var dp = Array(repeating: Array(repeating: 2, count: n), count: n)
var ans = 0
for i in 0..<n {
for j in 0..<i {
let delta = arr[i] - arr[j]
if let k = mp[delta], k < j {
dp[j][i] = dp[k][j] + 1
ans = max(ans, dp[j][i])
}
}
}
return ans > 2 ? ans : 0
}
}
|