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 ...
随机推荐
- day26——tyoe元类与object的联系、反射、函数与方法的区别、双下方法
day26 type元类与object联系 type 获取对象从属于的类 python 中一切皆对象, 类在某种意义上也是一个对象,python中自己定义的类,以及大部分内置类,都是由type元类(构 ...
- python鸭子类型
“当看到一只鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这只鸟就能够被称为鸭子” python的鸭子类型的设计让python的诸多对象的分类和应用更加灵活,需要注意类型和对象的区别(pytho ...
- ZooKeeper系列(四)—— Java 客户端 Apache Curator
一.基本依赖 Curator 是 Netflix 公司开源的一个 Zookeeper 客户端,目前由 Apache 进行维护.与 Zookeeper 原生客户端相比,Curator 的抽象层次更高,功 ...
- mysql中length与char_length字符长度函数使用方法
在mysql中length是计算字段的长度一个汉字是算三个字符,一个数字或字母算一个字符了,与char_length是有一点区别,本文章重点介绍第一个函数. mysql里面的length函数是一个用来 ...
- 过滤器 & 监听器 & 拦截器
过滤器: https://blog.csdn.net/MissEel/article/details/79351231 https://blog.csdn.net/qq_32363305/articl ...
- java注解注意点
注意:以后工作中代码中 不允许出现警告 自定义注解 1:自定义注解并没有发挥它的作用,而Eclipse自带的注解通过反射另外有一套代码,可以发挥它的作用,例如:跟踪代码...... 2:如果自定义的代 ...
- Linux:检查当前运行级别的五种方法
运行级就是Linux操作系统当前正在运行的功能级别.存在七个运行级别,编号从0到6.系统可以引导到任何给定的运行级别.运行级别由数字标识. 每个运行级别指定不同的系统配置,并允许访问不同的进程组合.默 ...
- CNN原理
卷积神经网络(Convolutional Neural Network)的结构类似于神经网络,可以看做是对其的改进.它利用局部连接.权值共享.多核卷积.池化四个手段大大降低了参数的数目,使得网络的层数 ...
- unity shader入门(一):基本结构话痨版
unity shader 有三种形式:表面着色器(Surface Shader),顶点/片元着色器(Vertex/Fragment Shader),固定函数着色器(Fixed Function Sha ...
- 刷脸支付袭来,WeChat Pay & AliPay争宠,究竟谁能笑到最后?
移动支付的快速发展,让我们摆脱了对现金的依赖,即使我们出门忘记带现金,那也没关系,我们照样可以通过手机来完成支付.现如今无论是大商场.还是水果摊都支持二维码付款,这也就意味着智慧化的生活正在一步步地向 ...