LC 873. 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].
Note:
3 <= A.length <= 10001 <= 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的更多相关文章
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- LeetCode 873. Length of Longest Fibonacci Subsequence
原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...
- 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 ...
- [LC] 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- Delphi 从一个对象中继承数据和方法
- 前端基础(一):HTML内容
HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...
- Linux--磁盘检查简单介绍
系统莫名其妙的掉电或磁盘发生问题非常可能导致文件系统的错乱,文件系统若发生错乱,可以使用fsck(file system check)命令进行检查. 使用权限:root用户 选项和参数: -a:检测到 ...
- 遍历二叉树 - 基于递归的DFS(前序,中序,后序)
上节中已经学会了如何构建一个二叉搜索数,这次来学习下树的打印-基于递归的DFS,那什么是DFS呢? 有个概念就行,而它又分为前序.中序.后序三种遍历方式,这个也是在面试中经常会被问到的,下面来具体学习 ...
- CAP理论概述
CAP理论 CAP原则,指在一个分布式系统中,Consistency(一致性).Availability(可用性).Partitiontolerance(分区容错性),三者不可同时拥有. 一致性(C) ...
- 基于IAP的STM32程序更新技术
引言 嵌入式系统的开发最终需要将编译好的代码下载到具体的微控制器芯片上,而不同厂家的微控制器芯片有不同的下载方式.随着技术的发展和应用需求的更新,用户程序加载趋向于在线编程的方式,越来越多的芯片公司提 ...
- python大数据初探--pandas,numpy代码示例
import pandas as pd import numpy as np dates = pd.date_range(',periods=6) dates import pandas as pd ...
- 队列BlockingQueue的简单例子
队列,当进行多线程编程的时候,很多时候可能会用到,队列是先进先出的,我们可以将要执行的任务放置在队列内缓存起来,当线程池中线程可以使用的时候,我们就从队列中获取一个任务执行.. 当前是一个队列的简单例 ...
- 单独使用ibatis做事物控制。
当项目中,只使用到了ibatis而没有使用spring来作为事物控制的时候,可以这样写: try { Reader reader = Resources.getResourceAsReader(&qu ...
- CodeForces 834C - The Meaningless Game | Codeforces Round #426 (Div. 2)
/* CodeForces 834C - The Meaningless Game [ 分析,数学 ] | Codeforces Round #426 (Div. 2) 题意: 一对数字 a,b 能不 ...