Length of Longest Fibonacci Subsequence LT873

Posted taste-it-own-it-love-it

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Length of Longest Fibonacci Subsequence LT873相关的知识,希望对你有一定的参考价值。

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

 Idea 1. Bruteforce, the key is two adjancent terms determins the next term, thought of using just one term ending at i and extending it, then realised that one term is not enough, two terms are needed. Starting with all pairs of terms (A[i] and A[j], 0 < i < j < A.length), (first, second) -> (second, first + second), build the array into a set and so we can quickly check if first+second exits or not.

Time complexity: O(N^2logM), M = max(A), since facotrial sequence is in exponential sequence, there are at most logM terms in the sequence.

Space complexity: O(N)

 1 class Solution {
 2     public int lenLongestFibSubseq(int[] A) {
 3         Set<Integer> target = Arrays.stream(A).boxed().collect(Collectors.toSet());
 4         
 5         int result = 0;
 6         for(int i = 0; i < A.length; ++i) {
 7             for(int j = i+1; j < A.length; ++j) {
 8                 int second = A[i] + A[j];
 9                 int first = A[j];
10                 int curr = 2;
11                 while(target.contains(second)) {
12                     int temp = second;
13                     second = first + second;
14                     first = temp;
15                     ++curr;
16                 }
17                 result = Math.max(result, curr);
18             }
19         }
20         
21         return result >= 3? result: 0;
22     }
23 }

Idea 2. Dynamic programming, since one term is not enough to extend the solution, why not use two terms? Let dp[i][j] represents the length of sequence ending at A[i] and A[j], to extend it

dp[j][k] = dp[i][j] + 1 when A[k] = A[i] + A[j]

dp[j][k] = 2

Time complexity: O(n^2)

Space complexity: O(n^2)

class Solution {
    public int lenLongestFibSubseq(int[] A) {
        Map<Integer, Integer> index = new HashMap<>();
        for(int i = 0; i < A.length; ++i) {
            index.put(A[i], i);
        }
        
        int result = 0;
        int[][] dp = new int[A.length][A.length];
        
        for(int k = 2; k < A.length; ++k) {
            for(int j = 1; j < k; ++j) {
                int i = index.getOrDefault(A[k] - A[j], A.length);
                if(i < j) {
                    dp[j][k] = dp[i][j] + 1;
                    result = Math.max(result, dp[j][k]);
                }
            }
        }
        
        return result >= 1? result + 2: 0;
    }
}

Idea 2.b save the map to store index, use two sum for sorted array

Time complexity: O(N^2)

Space complexity: O(N^2) or O(NlogM), M = max(A), worst case N = logM

 1 class Solution {
 2     public int lenLongestFibSubseq(int[] A) {
 3         int result = 0;
 4         Map<Integer, Integer> longest = new HashMap<>();
 5         int N = A.length;
 6         for(int k = 2; k < A.length; ++k) {
 7             for(int i = 0, j = k-1; i < j; ) {
 8                 if(A[i] + A[j] == A[k]) {
 9                     int cnt = longest.getOrDefault(i*N + j, 0) + 1;
10                     longest.put(j*N + k, cnt);
11                     result = Math.max(result, cnt);
12                     ++i;
13                     --j;
14                 }
15                 else if(A[i] + A[j] > A[k]) {
16                     --j;
17                 }
18                 else {
19                     ++i;
20                 }
21             }
22         }
23         
24         return result >= 1? result + 2: 0;
25     }
26 }

以上是关于Length of Longest Fibonacci Subsequence LT873的主要内容,如果未能解决你的问题,请参考以下文章

LC 873. Length of Longest Fibonacci Subsequence

Given a string, find the length of the longest substring without repeating characters.(给定一个字符串,找到最长的

pandas使用len函数和max函数计算dataframe中指定字符串数据列中字符串的最大长度(length of the longest string in a column)

673. Number of Longest Increasing Subsequence

leetcode 3. Longest Substring Without Repeating Characters (Python版)

leetcode1027. Longest Arithmetic Sequence