当前位置:网站首页>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;
}
}

边栏推荐
- Keras深度学习实战——基于Inception v3实现性别分类
- Is it necessary for project managers to take NPDP? I'll tell you the answer
- GBASE观察 | 数据泄露频发 信息系统安全应如何守护
- Version 2.0 of tapdata, the open source live data platform, has been released
- Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand
- 【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
- #797div3 A---C
- cv2读取视频-并保存图像或视频
- The body has a mysterious margin of 8px
- 发现值守设备被攻击后分析思路
猜你喜欢

XXL job of distributed timed tasks

保姆级教程:Azkaban执行jar包(带测试样例及结果)

Remote Sensing投稿經驗分享

How to fix the slip ring

Introduction à l'outil nmap et aux commandes communes

pb9.0 insert ole control 错误的修复工具

ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版

Android 创建的sqlite3数据存放位置

如何让导电滑环信号更好

The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
随机推荐
Redux使用
Is it necessary for project managers to take NPDP? I'll tell you the answer
云原生应用开发之 gRPC 入门
Remote Sensing投稿经验分享
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!!!
QML fonts use pixelsize to adapt to the interface
Gbase observation | how to protect the security of information system with frequent data leakage
Kwai applet guaranteed payment PHP source code packaging
PHP to get information such as audio duration
剑指 Offer II 041. 滑动窗口的平均值
Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand
Introduction à l'outil nmap et aux commandes communes
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘
保姆级教程:Azkaban执行jar包(带测试样例及结果)
burpsuite
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
C language -cmake cmakelists Txt tutorial
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)
PB9.0 insert OLE control error repair tool