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 }

原文地址:https://www.cnblogs.com/taste-it-own-it-love-it/p/10804246.html

时间: 2024-10-12 19:05:33

Length of Longest Fibonacci Subsequence LT873的相关文章

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 <= 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 on

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 <= 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 on

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

[LeetCode][JavaScript]Longest Increasing Subsequence

Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Not

LeetCode 300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description Submission Solutions Add to List Total Accepted: 64115 Total Submissions: 170859 Difficulty: Medium Contributors: Admin Given an unsorted array of integers, find the length of longest increasing subsequ

Longest Common Subsequence (DP)

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

Longest Common Subsequence Accepted : 7 Submit : 66 Time Limit : 3000 MS Memory Limit : 65536 KB Longest Common Subsequence Bobo has a sequence A=(a1,a2,-,an) of length n. He would like to know f(0),f(1),f(2) and f(3) where f(k) denotes the number of