当前位置:网站首页>leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
2022-07-08 00:37:00 【寒泉Hq】
题目
https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/
题解
被注释掉的部分是负优化。本意是O(n^2*logM)->O(n^2)
,然而对set做C(2,992)=491536次insert,比优化掉的logM开销还大。
class Solution {
public int lenLongestFibSubseq(int[] arr) {
int n = arr.length;
HashSet<Integer> set = new HashSet<>();
for (int a : arr) {
set.add(a);
}
int result = 0;
// HashSet<String> visited = new HashSet<>(); // "1,2"
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int x = arr[i];
int y = arr[j];
// String pair = x + "," + y;
int count = 2;
// if (!visited.contains(pair)) {
// visited.add(pair);
while (set.contains(x + y)) {
int next = x + y;
count++;
x = y;
y = next;
// visited.add(x + "," + y);
}
// }
result = Math.max(result, count);
}
}
return result > 2 ? result : 0;
}
}
边栏推荐
- 日志特征选择汇总(基于天池比赛)
- 如何让导电滑环信号更好
- Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- VIM string substitution
- [target tracking] |atom
- MySQL数据库(2)
- In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
- 快手小程序担保支付php源码封装
- 神经网络与深度学习-5- 感知机-PyTorch
猜你喜欢
随机推荐
Codeforces Round #643 (Div. 2)——B. Young Explorers
Cross modal semantic association alignment retrieval - image text matching
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
Introduction to Microsoft ad super Foundation
如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
WPF 自定义 写实风 雷达图控件
Nmap tool introduction and common commands
PHP to get information such as audio duration
Reading notes of Clickhouse principle analysis and Application Practice (7)
Dataworks duty table
Codeforces Round #643 (Div. 2)——B. Young Explorers
生态 | 湖仓一体的优选:GBase 8a MPP + XEOS
子矩阵的和
用户之声 | 对于GBase 8a数据库学习的感悟
2022国内十大工业级三维视觉引导企业一览
图解网络:揭开TCP四次挥手背后的原理,结合男女朋友分手的例子,通俗易懂
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Codeforces Round #649 (Div. 2)——A. XXXXX
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
php 获取音频时长等信息