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 <= 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++.)

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:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-873-length-of-longest-fibonacci-subsequence/

873. Length of Longest Fibonacci Subsequence的更多相关文章

  1. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  2. 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 ...

  3. LeetCode 873. Length of Longest Fibonacci Subsequence

    原题链接在这里:https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/ 题目: A sequence X_1, X ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  7. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  8. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  9. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

随机推荐

  1. Adplus 抓取Crash Dump

    本实例在win8.1 安装window kits https://developer.microsoft.com/en-us/windows/hardware/windows-driver-kit 1 ...

  2. NGS的duplicate的问题

    NGS的duplicate的问题   duplicate的三个问题: 一.什么是duplicate? 二.duplicate来源? 三.既然PCR将1个reads复制得到成百上千copies,那为什么 ...

  3. [NOI.AC]DELETE(LIS)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRMAAASJCAYAAABLtYu4AAAgAElEQVR4Xuzdf2xTd74n/PeqI/NsNB ...

  4. CSV 文件

    CSV 文件 CSV(Comma Separated Values 逗号分隔值) 是一种文件格式(如.txt..doc等),也可理解 .csv 文件就是一种特殊格式的纯文本文件.即是一组字符序列,字符 ...

  5. 深入应用c++11 随书代码

    代码并未在作者github上提供 将书中代码敲至vc 并调试运行 依赖BOOST库 编译环境vs2015 boost1.59 // Client.cpp : 定义控制台应用程序的入口点. // #in ...

  6. 新电脑的操作系统win10的所有设置问题汇总

    上来改的win7发现很多驱动没法装,装了也不能用,后来只能改win10了,另外win7的风扇声音也很大. 1.关闭win10自动更新.在服务里面禁用winupdate 2.注销改成了点头像,然后点注销 ...

  7. W-D-S-UART编程

    1.协议原理 2.原理框图 3.开发板底板与核心板图 4.开始配置寄存器 a).使相应I/O引脚配置为UART引脚 b).配置数据发送模式 c).设置为中断或查询模式 d).使能串口缓存 e).流量控 ...

  8. jquery统计显示或隐藏的元素个数

    统计显示的checkbox的数量: 统计隐藏的checkbox数量:

  9. 2018.10.19 NOIP训练 桌子(快速幂优化dp)

    传送门 勉强算一道dp好题. 显然第kkk列和第k+nk+nk+n列放的棋子数是相同的. 因此只需要统计出前nnn列的选法数. 对于前mmm%nnn列,一共有(m−1)/n+1(m-1)/n+1(m− ...

  10. 2018.08.16 洛谷P1437 [HNOI2004]敲砖块(二维dp)

    传送门 看起来普通dp" role="presentation" style="position: relative;">dpdp像是有后效性的 ...