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++.)

my solution.

Runtime: 935 ms, faster than 2.29% of Java online submissions for Length of Longest Fibonacci Subsequence.

我的思路,采用map,虽然也是dp,但是还是一个n2的时间复杂度,看了别人的做法,用的是头尾指针逼近,二分法。今天的周赛里判断数组成三角形的快速判断方法也是这样的。

class Solution {
public int lenLongestFibSubseq(int[] A) {
Map<Integer, Map<Integer,Integer>> mp = new HashMap<>();
int ret = ;
for(int i=; i<A.length; i++){
if(i == ) mp.put(A[i], new HashMap<>());
else {
mp.put(A[i],new HashMap<>());
Map<Integer,Integer> mpai = mp.get(A[i]);
for(int j=; j<i; j++){
Map<Integer,Integer> mpaj = mp.get(A[j]);
mpai.put(A[j], mpaj.getOrDefault(A[i]-A[j],)+);
ret = Math.max(ret, mpai.get(A[j]));
}
}
}
return ret > ? ret + : ;
}
}

网上的思路。

Runtime: 41 ms, faster than 94.29% of Java online submissions for Length of Longest Fibonacci Subsequence.

class Solution {
public int lenLongestFibSubseq(int[] A) {
if(A.length < ) return ;
int ret = ;
int[][] dp = new int[A.length][A.length];
for(int i=; i<A.length; i++){
int left = , right = i-;
while(left < right){
if(A[left] + A[right] == A[i]){
dp[right][i] = dp[left][right]+;
ret = Math.max(ret, dp[right][i]);
left++;
right--;
}else if(A[left] + A[right] > A[i]){
right--;
}else {
left++;
}
}
}
return ret == ? : ret + ;
}
}

LC 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. LeetCode 873. Length of Longest Fibonacci Subsequence

    原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...

  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. [LC] 300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  7. [LeetCode] Longest Increasing Subsequence 最长递增子序列

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

  8. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  9. Leetcode 300 Longest Increasing Subsequence

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

随机推荐

  1. 4.性能下降原因和常见的Join查询

    性能下降 SQL慢,执行时间长,等待时间长 1.查询语句写的烂 2.索引失效 单值索引失效 和 复合索引失效 3.关联查询太多join(设计缺陷或不得已的需求) 4.服务器调优及各个参数设置(缓冲.线 ...

  2. java EE,java Web中的400,404,405等各种错误介绍

    4 请求失败4xx 4xx应答定义了特定服务器响应的请求失败的情况.客户端不应当在不更改请求的情况下重新尝试同一个请求.(例如,增加合适的认证信息).不过,同一个请求交给不同服务器也许就会成功. 4. ...

  3. Oracle中函数关键字简介

    常用的语法:select--from--where--group by--having--order by 1.分组子句group by +con 按什么分组 2.having子句  对上面分组的数据 ...

  4. C - Calculation 2 HDU - 3501 (欧拉)

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  5. c语言啊

    双链表 quacklist 内核  模块加载

  6. 箭头函数与定时器的this指向问题

    函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象. 箭头函数本身没有this,this继承上级的this. 定时器中箭头函数的this指向包含定时器的函数,所以定时器中的箭头函数要 ...

  7. Gym - 102028H Can You Solve the Harder Problem? (后缀数组+RMQ+单调栈)

    题意:求一个序列中本质不同的连续子序列的最大值之和. 由于要求“本质不同”,所以后缀数组就派上用场了,可以从小到大枚举每个后缀,对于每个sa[i],从sa[i]+ht[i]开始枚举(ht[0]=0), ...

  8. hdu4786 Fibonacci Tree[最小生成树]【结论题】

    一道结论题:如果最小生成树和最大生成树之间存在fib数,成立.不存在或者不连通则不成立.由于是01图,所以这个区间内的任何生成树都存在. 证明:数学归纳?如果一棵树没有办法再用非树边0边替代1边了,那 ...

  9. summernote 富文本编辑器限制输入字符长度

    项目中需要一个比较简单的富文本编辑器,于是选中了summernote .虽然比较轻量,但是在开发中也遇到了几个问题,在此记录一下. 1:样式和bootstrap冲突,初始化之后显示为: .note-e ...

  10. Map遍历效率 : entrySet > keySet

     1    //entrySet()  2     for (Entry<String, String> entry : map.entrySet()) {  3         Stri ...