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 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 <= 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的更多相关文章
- 【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, ...
随机推荐
- 在php中连接数据库 pdo
在php中连接数据库 pdo //数据库信息 $mysql_conf = array( 'host' => '127.0.0.1', 'db' => 'meteorolog_foreign ...
- UCOSII 之 任务统计
UCOSII 使用空闲任务的计数值(OSIdleCtr)来实现CPU使用率的统计,首先统计一个固定时间内的计数值保存下来为 (MAX),然后再开启一个固定的时间段,当时间到达时得到另外一个(OSIdl ...
- 算法---Face_Recognition配置实战篇
python人脸识别库Face_Recognition-实操篇 @WP20190307 ================================目 录===================== ...
- docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /tmp/tfserving/
注意要是当前的完整路径 pwd查看到完整路径,再加入到source里面即可
- service worker在移动端H5项目的应用
1. PWA和Service Worker的关系 PWA (Progressive Web Apps) 不是一项技术,也不是一个框架,我们可以把她理解为一种模式,一种通过应用一些技术将 Web App ...
- IDEA 2019.1版本 永久激活(假装是永久激活,其实还是有时间的,不过这个时间比较长)
先下载 一个 jar 包 链接: https://pan.baidu.com/s/12eC8OZzMrUve2xC7aRUKLQ 提取码: bavi 将下载下来的jar包,放置你安装idea的目录的b ...
- Python3-list
list = ['abcd', 786, 2.23, 'runoob', 70.2] tinylist = [123, 'runoob'] print(list) # 输出完整列表 print(lis ...
- Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)
链接: https://codeforces.com/contest/1221/problem/D 题意: You have a fence consisting of n vertical boar ...
- 4、docker镜像:花卷结构、commit镜像
1.是什么 docker images 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量和配置文件. ...
- HDU 6076 - Security Check | 2017 Multi-University Training Contest 4
/* HDU 6076 - Security Check [ DP,二分 ] | 2017 Multi-University Training Contest 4 题意: 给出两个检票序列 A[N], ...