[Dynamic Programming Question 15] The longest definite difference subsequence && the length of the longest Fibonacci subsequence

1218. Longest definite difference subsequence

Link: 1218. Longest definite difference subsequence

Given an integer array arr and an integer difference, please find and return the length of the longest arithmetic subsequence in arr, where the difference between adjacent elements in the subsequence is equal to difference.

A subsequence is a sequence derived from arr by deleting some elements or not deleting any elements without changing the order of the remaining elements.

Example 1:

Input: arr = [1,2,3,4], difference = 1
Output: 4
Explanation: The longest arithmetic subsequence is [1,2,3,4].
Example 2:

Input: arr = [1,3,5,7], difference = 1
Output: 1
Explanation: The longest arithmetic subsequence is any single element.
Example 3:

Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2
Output: 4
Explanation: The longest arithmetic subsequence is [7,5,3,1] .

1. Status representation *
dp[i] represents: among all subsequences ending with the element at position i, the length of the longest arithmetic subsequence

2. State transition equation
For dp[i], the value of the previous fixed difference subsequence is set as arr[i] - difference. Just find dp[arr[i] - difference] of the length of the definite difference subsequence ending with the above number, and then add 1, which is the length of the definite difference subsequence ending with i.
Therefore, you can choose to use a hash table for optimization. We can bind "element, dp[j]" and put it into the hash table. You don't even need to create a dp array, just do dynamic programming directly in the hash table.

3. Initialization
At the beginning, you need to put the first element into the hash table, hash[arr[0]] = 1 4. The
order of filling in the form
is easy to see, and the order of filling in the form is "from left to right"

5. Return value
According to the "status indication", return the maximum value
code in the entire dp table:

 int longestSubsequence(vector<int>& arr, int difference) {
    
    
        unordered_map<int, int> hash;
        hash[arr[0]] = 1; 
        int ret = 1;
        for(int i = 1; i < arr.size(); i++)
        {
    
    
        hash[arr[i]] = hash[arr[i] - difference] + 1;
        ret = max(ret, hash[arr[i]]);
        }
        return ret;

    }

Insert image description here

873. The length of the longest Fibonacci subsequence

Link: 873. The length of the longest Fibonacci subsequence
The sequence X_1, X_2, …, X_n is said to be Fibonacci-like if it satisfies the following conditions:

n >= 3
For all i + 2 <= n, there is X_i + X_{i+1}
= The length of the Bonacci subsequence. If one does not exist, return 0.

(Recall that a subsequence is derived from the original sequence arr by removing any number of elements from arr (or none) 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: arr = [1,2,3,4,5,6,7,8]
Output: 5
Explanation: The longest Fibonacci subsequence is [1,2,3,5,8].
Example 2:

Input: arr = [1,3,7,11,12,14,18]
Output: 3
Explanation: The longest Fibonacci subsequences are [1,11,12], [3,11,14] and [7,11,18].


dp[i][j] represents: the length of the longest Fibonacci subsequence among all subsequences ending with the elements at position i and j. Stipulate that i < j

1. State representation *
dp[i][j] represents: the length of the longest Fibonacci subsequence among all subsequences ending with the elements at position i and j.

2. State transition equation
Assume nums[i] = b, nums[j] = c, then the first element of this sequence is a = c - b. We
discuss according to the situation of a:

  1. i. a exists, the subscript is k, and a < b: At this time, we need the length of the longest Fibonacci subsequence ending with the element at position k and position i, and then add the element at position j, that
    is Can. So dp[i][j] = dp[k][i] + 1;
  2. ii. a exists, but b < a < c: At this time, only two elements can play by themselves, dp[i][j] = 2;
  3. iii. a does not exist: At this time, only two elements can still play by themselves, dp[i][j] = 2.

3. Initialization
You can initialize all the values ​​in the table to 2.

4. The order of filling in the table is
a. Fix the last number first;
b. Then enumerate the second to last number.

5. Return value
Because we don’t know who the final result ends with, the maximum value ret in the dp table is returned.
But ret may be less than 3. If it is less than 3, it means it does not exist.
Therefore, it is necessary to judge

Code:

 int lenLongestFibSubseq(vector<int>& arr) {
    
    
        int n=arr.size();

        unordered_map<int,int> hash;
        for(int i=0;i<n;i++) hash[arr[i]]=i;

        vector<vector<int>> dp(n,vector<int>(n,2));
        int len=2;
        for(int i=2;i<n;i++)
        {
    
    
            for(int j=1;j<i;j++)
            {
    
    
                int a=arr[i]-arr[j];
                if(a<arr[j]&&hash.count(a))
                {
    
    
                    dp[i][j]=dp[j][hash[a]]+1;
                }
                len=max(len,dp[i][j]);
            }
        }
        return len<3?0:len;
        }

Insert image description here

Guess you like

Origin blog.csdn.net/m0_64579278/article/details/132919317