-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path873.cpp
More file actions
28 lines (28 loc) · 757 Bytes
/
873.cpp
File metadata and controls
28 lines (28 loc) · 757 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
28
class Solution {
public:
int length(long long x, long long y, unordered_set<long long>& st) {
int size = 2;
bool flag = false;
while (st.find(x + y) != st.end()) {
flag = true;
size++;
int temp = x + y;
x = y;
y = temp;
}
if (flag) return size;
return 0;
}
int lenLongestFibSubseq(vector<int>& arr) {
int n = arr.size();
unordered_set<long long> st;
for (auto& num : arr) st.insert(num);
int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
res = max(res, length(arr[i], arr[j], st));
}
}
return res;
}
};