Length of Longest Fibonacci Subsequence - Complete Solution Guide
Length of Longest Fibonacci Subsequence is LeetCode problem 873, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 arr of positive integers forming a sequence, return the length of the longest Fibonacci-like subsequence of arr . If one does not exist, return 0 . A subsequence is derived from another sequence arr by deleting any number of elements (including none) from arr , without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4,
Detailed Explanation
The problem asks us to find the length of the longest Fibonacci-like subsequence within a given strictly increasing array of positive integers. A Fibonacci-like sequence has at least three elements, and each element (after the first two) is the sum of the two preceding elements. The subsequence doesn't need to be contiguous, meaning we can pick and choose elements from the original array as long as their order is maintained.
Solution Approach
The solution uses dynamic programming to find the length of the longest Fibonacci-like subsequence. A hash table is used to quickly look up the index of each number in the array. The algorithm iterates through all possible pairs of numbers in the array, and for each pair, it checks if their difference is also in the array and smaller than the first number in the pair. If it is, then a Fibonacci-like subsequence has been found, and the length of the subsequence is updated in the dp table. The maximum length found is then returned.
Step-by-Step Algorithm
- Step 1: Create a hash table (val_to_index) to store the index of each value in the input array.
- Step 2: Initialize a dynamic programming table (dp) to store the lengths of Fibonacci-like subsequences. In Python, Java, and C++, the dp table is a hashmap storing lengths against key (i,j). In C, it's a 2D array dp[i][j].
- Step 3: Iterate through all possible pairs of indices (i, j) in the array, where i < j.
- Step 4: Calculate the 'target' value: target = arr[j] - arr[i].
- Step 5: Check if the target value is less than arr[i] and exists in the val_to_index hash table.
- Step 6: If the target value exists, retrieve its index 'k' from the val_to_index table.
- Step 7: Update the dp table. In Python, Java, C++, retrieve dp[(k, i)] if available, default to length = 2. Add 1 to the length and set dp[(i, j)] to this new value. In C, length is 2 if dp[k][i] is 0, otherwise it's dp[k][i] + 1, and then dp[i][j] is set to this length.
- Step 8: Update the max_len with the maximum length found so far.
- Step 9: After iterating through all pairs, return the max_len.
Key Insights
- Insight 1: The array is strictly increasing, which allows for efficient searching and eliminates the need to check for duplicates or maintain order when creating subsequences.
- Insight 2: Dynamic Programming is suitable because the length of the Fibonacci-like subsequence ending at indices i and j depends on the length of the Fibonacci-like subsequence ending at indices k and i, where arr[k] + arr[i] = arr[j].
- Insight 3: Using a hash table (or map) to store the index of each element in the array allows for fast lookups when searching for the third number in the Fibonacci-like sequence.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
Topics
This problem involves: Array, Hash Table, Dynamic Programming.
Companies
Asked at: Baidu.