原题链接在这里:https://leetcode.com/problems/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 <= 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;
}
}

类似Fibonacci Number.

LeetCode 873. Length of Longest Fibonacci Subsequence的更多相关文章

  1. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  8. [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 ...

  9. LeetCode 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

随机推荐

  1. cannot access org.springframework.core.io.InputStreamSouce

    cannot access org.springframework.core.io.InputStreamSouce错误,把mian路径下main.iml文件备份一下,然后删除该文件,报错就会消失,但 ...

  2. 洛谷P5022 旅行 题解

    前面几个代码都是部分分代码,最后一个才是AC了的,所以最后一个有详细注释 安利一发自己的Blog 这是提高组真题,233有点欧拉回路的感觉. 题目大意: 一个 连通 图,双向边 ,无重边 , 访问图中 ...

  3. 深度学习-CNN+RNN笔记

    以下叙述只是简单的叙述,CNN+RNN(LSTM,GRU)的应用相关文章还很多,而且研究的方向不仅仅是下文提到的1. CNN 特征提取,用于RNN语句生成图片标注.2. RNN特征提取用于CNN内容分 ...

  4. WPF 使用 AppBar 将窗口停靠在桌面上,让其他程序不占用此窗口的空间(附我封装的附加属性)

    原文:WPF 使用 AppBar 将窗口停靠在桌面上,让其他程序不占用此窗口的空间(附我封装的附加属性) 本文介绍如何使用 Windows 的 AppBar 相关 API 实现固定停靠在桌面上的特殊窗 ...

  5. Mysql表字段命令alter add

    alter add命令用来增加表的字段. alter add命令格式:alter table 表名 add字段 类型 其他; 例如,在表MyClass中添加了一个字段passtest,类型为int(4 ...

  6. ASP.NET WebApi 学习与实践系列(2)---WebApi 路由请求的理解

    目录 写在前面 WebApi Get 请求 1.无参数的请求 2.一个参数的请求 3.多个参数的请求 4.实体参数的请求 WebApi Post 请求 1.键值对请求 2.dynamic 动态类型请求 ...

  7. python 跨目录访问文件

    1.同级.同目录的文件之间的访问 有这样一个目录结构 假如,in_A.py 这个文件想调用 hello_world.py 中的函数怎么办呢? --->>>  import 只需在 i ...

  8. Fedora 30系统的升级方法

    Fedora 30 已经发布了.你可能希望将系统升级到最新版本的 Fedora.Fedora 工作站版本有图形化升级的方法.另外,Fedora 也提供了一个命令行方法,用于将 Fedora 29 升级 ...

  9. 如何统一管理单个任务下所有API的同步情况?

    1. 一分钟完成单个API配置 单个API的配置包含:API名称.URL地址.请求方式.参数设置.自定义高级设置. 参数允许用户填写:Text.WebService.Timestamp.DependO ...

  10. DataPipeline如何实现数据质量管理?

    数据质量管理已经成为数据治理的重要组成部分.高质量的数据是企业进行决策的重要依据. DataPipeline数据质量平台整合了数据质量分析.质量校验.质量监控等多方面特性, 以保证数据质量的完整性.一 ...