LeetCode 873. Length of Longest Fibonacci Subsequence
原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/
题目:
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3X_i + X_{i+1} = X_{i+2}for alli + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
题解:
Take a = A[i] and b = A[j] as first 2 elements, j>i.
Check if A contains A[i]+A[j]. If yes, then a = b, b = a+b, and fibonacci sequence length ++.
Until A doesn't contian (A[i] + A[j]), update the global longest length.
Time Complexity: O(n^2 * logM). n = A.length. M is the largest number in A, since in while loop it grows exponentially, while takes logM.
Space: O(n).
AC Java:
class Solution {
public int lenLongestFibSubseq(int[] A) {
if(A == null || A.length < 3){
return 0;
}
HashSet<Integer> hs = new HashSet<>();
for(int a : A){
hs.add(a);
}
int res = 0;
int n = A.length;
for(int i = 0; i<n-2; i++){
for(int j = i+1; j<n-1; j++){
int a = A[i];
int b = A[j];
int l = 2;
while(hs.contains(a+b)){
int temp = a;
a = b;
b = temp+b;
l++;
}
res = Math.max(res, l);
}
}
return res > 2 ? res : 0;
}
}
LeetCode 873. Length of Longest Fibonacci Subsequence的更多相关文章
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LC 873. Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- 873. Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- [LeetCode] Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- [Swift]LeetCode873. 最长的斐波那契子序列的长度 | Length of Longest Fibonacci Subsequence
A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode&Python] Problem 594. Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
随机推荐
- 【Tyvj2046】掷骰子
好水一道题 掷骰子Description Rainbow和Freda通过一次偶然的机会来到了魔界.魔界的大门上赫然写着:小盆友们,欢迎来到魔界~!乃们需要解决这样一个问题才能进入哦lala~有N枚骰子 ...
- delphi xe6 窗口 visible 不能隐藏 解决
delphi xe6 窗口 visible 不能隐藏 解决 在工程代码里面加上 Application.ShowMainForm := false;
- C#直接调用.mdf文件
一般情况下,.mdf文件都是作为MSSQL的数据库文件,只有在安装了Microsoft SQL Server才能实现调用. 事实上,除此之外,也可以直接调用.mdf文件,而无需安装Microsoft ...
- 关于使用jquery form submit出现多次提交的问题
错误的写法: $(this).submit(function () { $(this).ajaxSubmit({ url: opts.url, type: 'post', dataType: 'jso ...
- kali之HexorBase数据库破解
HexorBase 用户名密码连接数据库 暴力破解 点击底栏 Bruteforces Databases Servers , 然后会弹出一个新界面 Databases Bruteforces 新界面 ...
- loadrunner通过字符串左右边界提取字符串
/****** *函数名称:strcut *函数说明:通过左边界.右边界,从字符串中截取子字符串 *注意事项:会申请新的内存,需要手动释放 ******/ void strcut(char *strS ...
- CCProxy代理
只要局域网内有一台机器能够上网,其他机器就可以通过这台机器上安装的CCProxy来代理共享上网,最大程度的减少了硬件费用和上网费用.只需要在服务器上CCProxy代理服务器软件里进行帐号设置,就可以方 ...
- MongoDB 设置参数
服务器配置文件分析 bin目录下的mongod.cfg是服务器的配置文件,文件中主要的配置参数: 1.数据库文件的存放位置 2.服务器日志文件的存放位置 3.默认的IP地址.端口号 设置密码 默认情况 ...
- 元素定位方法之Uiautomator方法
这个方法只能用于安卓系统,方法通过类UiSelector()来构造对象的 官网地址:https://developer.android.google.cn/topic/libraries/testin ...
- sphinx中文版Coreseek中文检索引擎安装和使用方法(Linux)
sphinx中文版Coreseek中文检索引擎安装和使用方法(Linux) 众所周知,在MYSQL数据库中,如果你在百万级别数据库中使用 like 的话那你一定在那骂娘,coreseek是一个 ...