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 ...
随机推荐
- 三、oracle 用户管理一
三.oracle 用户管理一 一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 ide ...
- 一、消息队列之ActiveMQ的安装、配置和C#样例代码
最近有时间了,研究一下消息队列ActvieMQ,结合自己的实践和网上的一些大家内容,整理如下,所有步骤和链接均是正确的. 1.ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲 ...
- 面向对象设计模式纵横谈:Builder 生成器模式(笔记记录)
Builder模式的缘起 假设创建游戏中的一个房屋House设施,该房屋的构建由几个部分组成,且各个部分要富于变化. 如果使用最直观的设计方法,每一个房屋部分的变化,都将导致房屋构建的重新修正…… 动 ...
- binlog怎样参与mysql recover的
转自 Louis Hust's Blog MySQL两阶段提交 29 July 2015 参数介绍 两阶段提交 什么情况下会出现binlog写入了,但是实际这条数据不存在库中? 参数介绍 innod ...
- HDFS高可用性及其分布式系统思想基础
源自单点失效问题,也就是当NameNode不可用的时候,用什么办法可以平滑过渡? 最直接的办法是再添加一个备用的NN,这就产生了Active NameNode和Standby NameNode的设计思 ...
- 设计社区类Web原型制作分享-Behance
Behance 是著名设计社区,创意设计人士可以展示自己的作品,发现别人分享的创意作品. 网站有二级导航,主要用到的交互组件有弹出面板,通过弹出面板来隐藏展现搜索框.并且用到的组件有播放器.菜单栏.下 ...
- python函数、装饰器、迭代器、生成器
目录: 函数补充进阶 函数对象 函数的嵌套 名称空间与作用域 闭包函数 函数之装饰器 函数之迭代器 函数之生成器 内置函数 一.函数补充进阶 1.函数对象: 函数是第一类对象,即函数可以当作数据传递 ...
- Oracle SQL 硬解析和子游标
Oracle SQL 硬解析和子游标 What reasons will be happening sql hard parse and generating new child cursors 在一 ...
- android 网站上下的 adt 不能显示没有安装的
问题描述 使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository ...
- unidac 6.0.1 与kbmmw 的一点小摩擦
unidac 6.0.1 出来了,虽然支持sql server 直连等新特性,但是由于内部改动比较大, 导致与kmmmw 的集成起来存在有点小问题,就是如果数据库不是interbase 或者fire ...