英文版
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].

Note:

  • - 3 <= A.length <= 1000
  • - 1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9

(The time limit has been reduced by 50% for submissions in Java, C, and C++.)

中文版:
给你一个严格单调递增的数组,请问数组里最长的斐波那契序列的长度是多少?例如,如果输入的数组是[1, 2, 3, 4, 5, 6, 7, 8],由于其中最长的斐波那契序列是1, 2, 3, 5, 8,因此输出应该是5。

分析:

思路一
在斐波那契序列中,第n个数字等于第n-1个数字与第n-2个数字之和。

考虑以数组中第i个数字(记为A[i])为结尾的最长斐波那契序列的长度。对于每一个j(0 <= j < i),A[j]都有可能是在某个斐波那契序列中A[i]前面的一个数字。如果存在一个k(k < j)满足A[k] + A[j] = A[i],那么这三个数字就组成了一个斐波那契序列。这个以A[i]为结尾、前一个数字是A[j]的斐波那契序列是在以A[j]为结尾、前一个数字是A[k]的序列的基础上增加了一个数字A[i],因此前者的长度是在后者的长度基础上加1。

我们可以用一个二维数组lengths来记录斐波那契序列的长度。二维数组中第i行第j列数字的含义是以输入数组中A[i]结尾、并且前一个数字是A[j]的斐波那契序列的长度。如果存在一个数字k,满足A[k] + A[j] = A[i],那么lengths[i][j] = lengths[j][k] + 1。如果不存在满足条件的k,那么意味这A[j]、A[i]不在任意一个斐波那契序列中,lengths[i][j]等于2。

二维数组lengths中的最大值就是输出值。

 class Solution {
public int lenLongestFibSubseq(int[] A) {
if (null == A || A.length == 0) {
return 0;
}
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < A.length; i ++) {
map.put(A[i], i);
} int[][] lengths = new int[A.length][A.length];
int maxLength = 1;
for (int i = 1; i < A.length; i ++) {
int num_3 = A[i];
int length = 2;
for (int j = i-1; j >= 0; j --) {
int num_2 = A[j];
int num_1 = num_3 - num_2; int len = 2;
if (num_1 < num_2 && map.containsKey(num_1)) {
len = lengths[j][map.get(num_1)] + 1;
}
lengths[i][j] = len;
length = Math.max(length, len);
}
maxLength = Math.max(maxLength, length);
}
return maxLength > 2 ? maxLength : 0;
}
}

思路二
双重循环枚举所有可能的情况

 class Solution {
public int lenLongestFibSubseq(int[] A) {
int N = A.length;
Set<Integer> S = new HashSet();
for (int x: A) S.add(x); int ans = 0;
for (int i = 0; i < N; ++i)
for (int j = i+1; j < N; ++j) {
int x = A[j], y = A[i] + A[j];
int length = 2;
while (S.contains(y)) {
// x, y -> y, x+y
int tmp = y;
y += x;
x = tmp;
ans = Math.max(ans, ++length);
}
} return ans >= 3 ? ans : 0;
}
}

最长斐波那契序列-LeetCode-873的更多相关文章

  1. [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  2. HDU 5620 KK's Steel (斐波那契序列)

    KK's Steel 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/J Description Our lovely KK ha ...

  3. pytho查找斐波那契序列中的值

    ''' 实现斐波那契序列,查找其中第N个数的值 ''' def FeiBSequence(list,N): length=len(list); i=0; while i<length: if N ...

  4. 爬楼梯问题-斐波那契序列的应用.md

    N 阶楼梯,一次可以爬1.2.3...n步,求爬楼梯的种类数 /** * 斐波那契序列 */ public class ClimbingStairs { // Sol 1: 递归 // 递归 公式:F ...

  5. 利用python实现二分法和斐波那契序列

    利用python实现二分法:我的实现思路如下 1.判断要查找的值是否大于最大值,如果大于则直接返回False 2.判断要查找的值是否小于最小值,如果小于则直接返回False 3.如果要查找的值在最大值 ...

  6. 【严蔚敏】【数据结构题集(C语言版)】1.17 求k阶斐波那契序列的第m项值的函数算法

    已知k阶斐波那契序列的定义为 f(0)=0,f(1)=0,...f(k-2)=0,f(k-1)=1; f(n)=f(n-1)+f(n-2)+...+f(n-k),n=k,k+1,... 试编写求k阶斐 ...

  7. 【剑指offer】斐波那契序列与跳台阶

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25337983 剑指offer上的第9题,简单题,在九度OJ上測试通过. 主要注意下面几点: ...

  8. [Swift]LeetCode842. 将数组拆分成斐波那契序列 | Split Array into Fibonacci Sequence

    Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...

  9. 高精度处理斐波那契序列(C语言)

    #include<stdio.h> #include<string.h> //memset,strcpy,strlen函数头文件 int main(void) { ];//用来 ...

随机推荐

  1. web应用配置虚拟路径映射方式一配置不成功问题解决办法

    配置过程图: 为了方便输入,先修改Tomcat的conf文件下的server.xml文件 默认端口修改为80 配置完成之后访问报404错误解决办法: 一.如果配置前已经开启了Tomcat服务器,配置完 ...

  2. POJ 1270

    #include<iostream> #include<algorithm> #define MAXN 26 #define MAX 300 using namespace s ...

  3. AngularJS自定义Directive

    (编辑完这篇之后,发现本篇内容应该属于AngularJS的进阶,内容有点多,有几个例子偷懒直接用了官方的Demo稍加了一些注释,敬请见谅). 前面一篇介绍了各种常用的AngularJS内建的Direc ...

  4. VSTO学习(六)——创建Outlook解决方案

    本专题概要 引言 Outlook对象模型 自定义Outlook窗体 小结 一.引言 在上一个专题中,为大家简单介绍了下如何创建Word解决方案的,所以本专题中将为大家介绍下Outlook相关的内容.我 ...

  5. Lingo 做线性规划 - Financial Applications

    Reference: <An Introduction to Management Science Quantitative Approaches to Decision Making, Rev ...

  6. (转)更换镜像rootvg卷组中的硬盘

    F85系统镜像盘更换实录之一:删除原有镜像操作 # cfgmgr # lsdev -Cc disk hdisk0 Available 11-09-00-8,0  16 Bit LVD SCSI Dis ...

  7. linux中查找某端口,并关闭对应的端口

    1,netstat -ntlp  (n表示不反向域名杰斯 t表示查看tcp协议的连接 l查看正在监听端口 p获取进程号和端口) 2,然后直接kill -9 端口号 参考全文:https://linux ...

  8. 微服务Kong(十)——负载均衡参考

    KONG为请求多个后端服务提供了多种负载均衡方案:一种是简单的基于DNS,另一种是更加动态的环形均衡器,他在不需要DNS服务器的情况下也允许服务注册. 一.基于DNS的负载均衡 当使用基于DNS的负载 ...

  9. chroot的用法

    chroot命令用来在指定的根目录下运行指令.chroot,即 change root directory (更改 root 目录).在 linux 系统中,系统默认的目录结构都是以/,即是以根 (r ...

  10. NLP 装桶(Bucketing)和填充(padding)

    翻译模型也是用了装桶(bucketing)和填充(padding),这两种方法是用于高效地处理不同长度句子的情况.我们首先来弄清楚是怎么一回事.当我们从英语翻译成法语的时候,假设我们的输入英语的长度为 ...