forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0873.cpp
More file actions
27 lines (27 loc) · 738 Bytes
/
Copy path0873.cpp
File metadata and controls
27 lines (27 loc) · 738 Bytes
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
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int lenLongestFibSubseq(vector<int>& A)
{
unordered_set<int> S(A.begin(), A.end());
long long res = 0;
for (int i = 0; i < A.size(); ++i)
{
for (int j = i+1; j < A.size(); ++j)
{
int a = A[i], b = A[j];
long long l = 2;
if ((res - 2)*b > A.back()) break;
while (S.count(a + b))
{
b = a + b;
a = b - a;
++l;
}
res = max(res, l);
}
}
return res > 2 ? res : 0;
}
};