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 ofA
. 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) fromA
, 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++.)
Approach #1: unordered_map. [C++]
class Solution {
public:
int lenLongestFibSubseq(vector<int>& A) {
unordered_map<int, int> memo;
int len = A.size();
int ans = 0, temp = 0;
for (int i = 0; i < len; ++i)
memo[A[i]] = i;
for (int i = 0; i < len; ++i) {
for (int j = i + 1; j < len; ++j) {
int ant = 2;
int last_idx = i;
for (int cur_idx = j; cur_idx < len; ) {
temp = A[last_idx] + A[cur_idx];
if (memo.count(temp)) {
ant++;
last_idx = cur_idx;
cur_idx = memo[temp];
} else break;
}
ans = max(ans, ant);
}
}
return ans == 2 ? 0 : ans;
}
};
Approach #2: DP. [Java]
class Solution {
public int lenLongestFibSubseq(int[] A) {
int n = A.length;
int res = 0;
int[][] dp = new int[n+1][n+1];
for (int[] row : dp) Arrays.fill(row, 2);
Map<Integer, Integer> pos = new HashMap<>();
for (int i = 0; i < n; ++i) pos.put(A[i], i);
for (int j = 2; j < n; ++j) {
for (int i = j-1; i > 0; --i) {
int prev = A[j] - A[i];
if (prev >= A[i]) break;
if (!pos.containsKey(prev)) continue;
dp[i][j] = dp[pos.get(prev)][i] + 1;
res = Math.max(res, dp[i][j]);
}
}
return res;
}
}
Analysis:
873. Length of Longest Fibonacci Subsequence的更多相关文章
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 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 ...
- LeetCode 873. Length of Longest Fibonacci Subsequence
原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...
- [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 ...
- [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, ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
随机推荐
- 766A Mahmoud and Longest Uncommon Subsequence
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- restful发布服务
概述 REST 从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表示方式.获得这些表徵致使这些应用程序转变了其状态.随着不断获取资源的表示方式,客户端应用 ...
- [ Laravel 5.5 文档 ] 底层原理 —— 一次 Laravel 请求的生命周期
Posted on 2018年3月5日 by 学院君 简介 当我们使用现实世界中的任何工具时,如果理解了该工具的工作原理,那么用起来就会得心应手,应用开发也是如此.当你理解了开发工具如何工作,用起 ...
- Jmeter If Controller中设置多个条件用“与”进行连接
"${noteID}"!="NOT FOUND" && "${securitiesId}"!="0P00011FQ ...
- 移动文件流的读写指针---fseek
函数原型:int fseek(FILE *stream,long offset,int origin) stream:文件指针, offset:偏移量,正数表示正向偏移,负数表示负向偏移.origin ...
- part1:15-安装Linux系统到开发板
1.Qtopia简介 Qtopia是Trolltech公司为采用嵌入式Linux操作系统的消费电子设备而开发的综合应用平台,Qtopia包含完整的应用层.灵活的界面用户.窗口操作系统.应用程序启动程序 ...
- 共享内存system v(未编译)
#include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> ...
- PTA第五次作业
#include<stdio.h> #include<math.h> int main () { int n,m,i,j,a; scanf("%d",&am ...
- 情境领导II
情境领导理论认为,领导者的行为要与被领导者的准备程度相适应,才能取得有效的领导效果,也就是说领导风格不是一成不变的,而要根据环境及员工的变化而改变. 三大技巧分别为诊断.弹性与约定领导型态.诊断是评估 ...
- 在使用html5的video标签播放视频时为何只有声音却没有图像
在使用html5的video标签播放视频时为何只有声音却没有图像? 答:使用格式化工厂转个编码就行了,MP4有3种编码,mpg4(xdiv),,mpg4(xvid),avc(h264)转换成H264编 ...