原题链接在这里: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. 大数据之路【第十四篇】:数据挖掘--推荐算法(Mahout工具)

    数据挖掘---推荐算法(Mahout工具) 一.简介 Apache顶级项目(2010.4) Hadoop上的开源机器学习库 可伸缩扩展的 Java库 推荐引擎(协同过滤).聚类和分类 二.机器学习介绍 ...

  2. python_并行与并发、多线程

    问题一: 计算机是如何执行程序指令的? 问题二: 计算机如何实现并发的? 轮询调度实现并发执行 程序1-8轮询完成,才再CPU上运行 问题三: 真正的并行需要依赖什么? 并行需要的核心条件 多进程实现 ...

  3. python 打包前三天日志

    日志格式 app-2019-07-24.log app-2019-07-24.1.log 该脚本适合一天之内有多个日志文件 # /usr/bin/python #-*- coding: utf-8 - ...

  4. Git diff (---和+++具体解释)(转)

    转自:https://blog.csdn.net/lovezbs/article/details/46492933

  5. HBase 系列(三)—— HBase 基本环境搭建

    一.安装前置条件说明 1.1 JDK版本说明 HBase 需要依赖 JDK 环境,同时 HBase 2.0+ 以上版本不再支持 JDK 1.7 ,需要安装 JDK 1.8+ .JDK 安装方式见本仓库 ...

  6. Deploy custom service on non hadoop node with Apache Ambari

    1   I want to deploy a custom service onto non hadoop nodes using Apache Ambari. I have created a cu ...

  7. 去掉a标签点击后的虚边框

    a { cursor: pointer; text-decoration: none; hide-focus: expression(this.hideFocus=true); outline: no ...

  8. DbParameter关于Like查询的传参数无效问题

    用传参方式模糊查询searchName 按常规的思路,我们会这样写 ,代码如下: String searchName ="Sam"; String strSql = "s ...

  9. SpringBoot启动原理详解

    SpringBoot和Spring相比,有着不少优势,比如自动配置,jar直接运行等等.那么SpringBoot到底是怎么启动的呢? 下面是SpringBoot启动的入口: @SpringBootAp ...

  10. Spring 在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...