当前位置:网站首页>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;
}
}
边栏推荐
- Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
- Remote Sensing投稿经验分享
- 剑指 Offer II 041. 滑动窗口的平均值
- powerbuilder 中使用线程的方法
- Applet running under the framework of fluent 3.0
- How to make the conductive slip ring signal better
- 快速熟知XML解析
- 分布式定时任务之XXL-JOB
- 2022国内十大工业级三维视觉引导企业一览
- 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!!!
猜你喜欢
ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
[SolidWorks] modify the drawing format
Remote Sensing投稿經驗分享
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
adb工具介绍
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
burpsuite
为什么更新了 DNS 记录不生效?
随机推荐
Codeforces Round #643 (Div. 2)——B. Young Explorers
Tencent game client development interview (unity + cocos) double bombing social recruitment 6 rounds of interviews
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
The body has a mysterious margin of 8px
Redisson distributed lock unlocking exception
How to realize batch control? MES system gives you the answer
Introduction à l'outil nmap et aux commandes communes
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!!!
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
#797div3 A---C
Uniapp one click Copy function effect demo (finishing)
C语言-模块化-Clion(静态库,动态库)使用
保姆级教程:Azkaban执行jar包(带测试样例及结果)
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
Partage d'expériences de contribution à distance
分布式定时任务之XXL-JOB
Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
Flutter 3.0框架下的小程序运行
Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)
Is NPDP recognized in China? Look at it and you'll see!