[Dynamic Programming Question 13] Longest Increasing Subsequence && Swing Sequence

300. Longest increasing subsequence

Link: 300. Longest increasing subsequence

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

2. State transition equation
For dp[i], we can conduct a classification discussion based on the "composition method of the subsequence":

  1. i. Subsequence length is 1: You can only play by yourself, at this time dp[i] = 1;
  2. ii. The length of the subsequence is greater than 1: nums[i] can follow any previous number to form a subsequence. Assume that the subscript of the previous number is j, where 0 <= j <= i - 1.

As long as nums[j] < nums[i], the element at position i follows the element j to form an increasing sequence with a length of
dp[j] + 1.
Therefore, we only need to find the largest dp[j] + 1 that meets the requirements.
To sum up, dp[i] = max(dp[j] + 1, dp[i]), where 0 <= j <= i - 1 && nums[j]< nums[i]

3. Initialization
All elements "individually" can form an increasing subsequence, so all elements in the dp table can be initialized to 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
Since we don’t know who the longest increasing subsequence ends with, the “maximum value” in the dp table is returned.

Code:

 int lengthOfLIS(vector<int>& nums) {
    
    
      int n=nums.size();

        vector<int> dp(n,1);
        int Max=1;
        for(int i=1;i<n;i++)
        {
    
    
            for(int j=0;j<i;j++)
            {
    
    
                if(nums[i]>nums[j])
                dp[i]=max(dp[j]+1,dp[i]);
            }
            Max=max(Max,dp[i]);
        }
        return Max;
    }

Insert image description here

376. Swing sequence

Link: 376. Swing Sequence

If the difference between consecutive numbers alternates strictly between positive and negative numbers, the sequence of numbers is called a wobble sequence. The first difference (if present) may be positive or negative. Sequences with only one element or two unequal elements are also considered wobble sequences.

For example, [1, 7, 4, 9, 2, 5] is a oscillating sequence because the difference (6, -3, 5, -7, 3) alternates between positive and negative.

On the contrary, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not swing sequences. The first sequence is because its first two differences are both positive numbers. The second sequence Because its last difference is zero.
A subsequence can be obtained by deleting some (or not) elements from the original sequence, leaving the remaining elements in their original order.

Given an integer array nums, return the length of the longest subsequence in nums as a wobble sequence.

Example 1:

Input: nums = [1,7,4,9,2,5]
Output: 6
Explanation: The entire sequence is a swing sequence, and the difference between each element is (6, -3, 5, -7, 3) .
Example 2:

Input: nums = [1,17,5,10,13,15,10,5,16,8]
Output: 7
Explanation: This sequence contains several wobble sequences of length 7.
One of them is [1, 17, 10, 13, 10, 16, 8], and the difference between the elements is (16, -7, 3, -3, 6, -8).
Example 3:

Input: nums = [1,2,3,4,5,6,7,8,9]
Output: 2

1. Status indication *

  1. f[i] means: among all the subsequences ending with the i position element, the length of the longest swing sequence in which the last position shows an "upward trend";
  2. g[i] represents: among all subsequences ending with the i position element, the length of the longest swing sequence in which the last position shows a "downward trend".

2.
Due to the special composition of the subsequence of the state transition equation, the i position is the ending subsequence, and the previous position can be any position in [0, i - 1], so let j be [0, i - 1] A certain position within the interval.
For f[i], we can conduct a classification discussion based on the "form of the subsequence":

  1. i. Subsequence length is 1: You can only play by yourself, at this time f[i] = 1
  2. ii. The length of the subsequence is greater than 1: Because the end needs to show an upward trend, nums[j] < nums[i] needs to be met. Under this condition, the end of j needs to show a downward trend. The longest swing sequence is g[ j] + 1
    Therefore we need to find the maximum g[j] + 1 under all satisfying conditions.

In summary, f[i] = max(g[j] + 1, f[i])please note that judgment is required when using g[j].
g[i] is similar.

3. Initialization
All elements "individually" can form a swing sequence, so all elements in the dp table can be initialized to 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. The return value
should return "the maximum value of the two dp tables". We can update a "maximum value" when filling in the form.

Code:

 int n=nums.size();

        vector<int> f(n,1),g(n,1);
        int Max=1;
        for(int i=1;i<n;i++)
        {
    
    
            for(int j=0;j<i;j++)
            {
    
    
                if(nums[i]>nums[j])
                {
    
    
                    f[i]=max(f[i],g[j]+1);
                }
                if(nums[i]<nums[j])
                {
    
    
                    g[i]=max(f[j]+1,g[i]);
                }
                Max=max(Max,max(g[i],f[i]));
            }
        }
        return Max;

Insert image description here

Guess you like

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