873. Length of Longest Fibonacci Subsequence
A sequence
X_1, X_2, ..., X_nis fibonacci-like if:
n >= 3X_i + X_{i+1} = X_{i+2}for alli + 2 <= nGiven a strictly increasing array
Aof 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
Aby 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 <= 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++.)
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 ...
随机推荐
- spring框架之数组,集合(List,Set,Map),Properties等的注入
先编写User类: package com.huida.demo4; import java.util.Arrays; import java.util.List; import java.util. ...
- Django之virtualenv下安装xadmin
1.安装xadmin,通过pip 进入virtualenv pip安装xadmin pyyuc:~ yuchao$ source PycharmProjects/mxvenv/bin/activate ...
- windows,phalcon工具的安装使用
一.使用工具之前,必须安装phalcon的扩展,也就是php_phalcon.dll动态链接库 phalcon官方地址:https://github.com/phalcon/cphalcon/rele ...
- Eclipse Gradle配置
一.Gradle简介 Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具. 二.配置步骤如下: 1.资源下载: Grandle官网下载G ...
- linux中如何检测设备驱动模块是否存在
linux系统中的设备驱动是否安装好一般检查几个方面:1.系统日志.嵌入式系统多是直接dmesg一下,看有没有设备关键字相关的出错信息(通用系统可检查/var/log/messages文件).2.已加 ...
- 抓取访客ip
$realip = $_SERVER['REMOTE_ADDR'] $proxy_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);//有用到代理 ...
- doctotext
文档解析库 http://www.it610.com/article/1936051.htm
- 什么是adb命令?以及如果高效使用他们?
接下来 我会为大家讲解 最通俗易懂的adb命令 希望大家能够喜欢...
- 2018.09.26 bzoj5221: [Lydsy2017省队十连测]偏题(数学推导+矩阵快速幂)
传送门 由于没有考虑n<=1的情况T了很久啊. 这题很有意思啊. 考试的时候根本不会,骗了30分走人. 实际上变一个形就可以了. 推导过程有点繁杂. 直接粘题解上的请谅解. 不得不说这个推导很妙 ...
- DevExpress gridcontrol Master-Detail绑定到对象类型
数据库:C_ProductPlan ,C_ProductPlanItemDTO定义:(实现每个计划条目-Master,对应多个ProcessInfo-Detail) [DataContract] [S ...