forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
32 lines (32 loc) · 870 Bytes
/
Solution.cpp
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
30
31
32
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;
}
};