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 ...
随机推荐
- 对于Android开发,啥是高级工程师?
最近一直在思考自己的技术方向.新的技术永远都是层出不穷,kotlin,flutter,小程序,轻应用等等,但是作为一个老鸟,新的东西,永远都是学不完的,想在新的技术上迭代学习出一个新高度,而增加自己的 ...
- bluez蓝牙测试工具
http://blog.csdn.net/talkxin/article/details/50610984
- 深入理解line-height与vertical-align(1)
http://www.cnblogs.com/xiaohuochai/p/5271217.html
- 【Linux】目录文件权限的查看和修改【转】
转载自:http://zhaoyuqiang.blog.51cto.com/6328846/1214718 ============================================== ...
- Determine overlapping rectangles
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- 2018.09.26 bzoj5221: [Lydsy2017省队十连测]偏题(数学推导+矩阵快速幂)
传送门 由于没有考虑n<=1的情况T了很久啊. 这题很有意思啊. 考试的时候根本不会,骗了30分走人. 实际上变一个形就可以了. 推导过程有点繁杂. 直接粘题解上的请谅解. 不得不说这个推导很妙 ...
- jedis 链接池使用(转)
Jedis作为redis的最佳客户端,它提供了连接池的特性,“连接池”在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apache commons-po ...
- Linux各个版本资源下载
Linux系统各发行版镜像下载(持续更新) == Linux系统各发行版镜像下载(2014年10月更新),如果直接下载不了,请使用迅雷下载.并且注意,我的下载地址,在 迅雷 里才起作用. Linux ...
- /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory
https://stackoverflow.com/questions/39111930/usr-include-boost-python-detail-wrap-python-hpp5023-fat ...
- 使用Volley上传文件
使用浏览器上传文件,然后通过Wireshark抓包分析,发现发送的数据大概是这个样子. MIME Multipart Media Encapsulation, Type: multipart/form ...