[Algorithm Beating Diary] day31——673. The number of the longest increasing subsequence, 646. The longest number pair chain

 673. Number of longest increasing subsequences

673. Number of longest increasing subsequences

Question analysis:

Given an unsorted integer array nums , returns the number of the longest increasing subsequence .

Note This sequence must be strictly increasing.

Problem-solving ideas:

Algorithm idea:
1. Status display:
First try to define a state: the "number" of the longest increasing subsequence ending with i . Then the problem comes, I don’t even know
What is the "length" of the longest increasing subsequence ending with i ? How do I know the longest increasing subsequence? What about the number?
Therefore, we need two states to solve this problem, one is "length" and the other is "number":
len[i] Display: Below i In order of the last most popular Longitude;
count[i] means: the longest increasing subsequence ending with i number.
2. State transfer process:
Before finding the number, we must first know the length, so first look at len[i] :
i. Searching i The last time the order increases Knowledge [0, i - 1] ward len[j]
Information, use j Display [0, i - 1] < a i=4>District top title;
ii. My demand is the order of increase, so this [0, i - 1] District Upper level nums[j] Tadakamonowa nums[i]
forms an ascending sequence, then the value of dp[i] can be updated. At this time, the maximum length is < a i=3>dp[j] + 1 ;
iii. What we want is [0, i - 1] in all cases The maximum value.
In summary, for len[i] , we can get the state transition equation as:
len[i] = max(len[j] + 1, len[i]) , inside 0 <= j < ; i ,并且 nums[j] <
nums[i]
When we know the length of the longest increasing subsequence at the end of each position, let’s see if we can get count[i] :
i. I know this time len[i] 's information, I know the way a> [0, i - 1] counterwise count[j] trust < /span>
breath, use j display [0, i - 1] < a i=4>District top title;
ii. We can traverse it again [0, i - 1] on the interval All elements, as long as they can form an ascending sequence and go up
The length of the ascending sequence is equal to dp[i] , then we take count[i ] plus the value of count[j] . Cycle like this
After one loop, count[i] stores the value we want.
In summary, for count[i] , we can get the state transfer equation as:
count[i] += count[j] ,其中 0 <= j < i ,并且 nums[j] < nums[i] &&
dp[j] + 1 == dp[i]
3. Initialization:
For len[i] , all elements by themselves can form an ascending sequence , directly initialize all to 1 ;
for count[i] , if all initialized to 1 , when accumulating, "not the maximum length situation" may be accumulated
is added, so we can first initialize it to 0 , and then make a judgment when accumulating. The specific operation depends on the code
code~
4. Filling order:
There is no doubt that it is "from left to right".
5. Reply:
Use manLen to represent the length of the final longest increasing subsequence.
According to the question requirements, we should return the number of all subsequences whose length is equal to maxLen .

 Solution code:

class Solution {
public:
    int findNumberOfLIS(vector<int>& nums) {
        int n=nums.size();
        vector<int>dp(n,1);
        vector<int>f(n,1);
        int retlength=1;
        int retcount=1;
        for(int i=1;i<n;i++)
        {
            //int length=f[0];//0到i-1区间内的最大长度
            for(int j=0;j<i;j++)
            {
                if(nums[j]<nums[i])
                {       
                    if(f[j]+1==f[i])dp[i]+=dp[j];
                    else if(f[j]+1>f[i])
                    {
                        dp[i]=dp[j];
                        f[i]=f[j]+1;
                    }
                }  
            }
            if(retlength==f[i])retcount+=dp[i];
                else if(retlength<f[i])
                {
                    retcount=dp[i];
                    retlength=f[i];
                }
        }
       
        return retcount; 
    }
};

646. The longest pair chain

​​​​​​646. The longest number pair chain

Topic description:

Give you a pair array consisting of n pairs pairs , where pairs[i] = [lefti, righti] and lefti < righti .

Now, we define a following relationship, if and only if b < c , the number pair < /span>  . number pairs . We use this form to construct a chain of p2 = [c, d] can follow p1 = [a, b]

Find and return the length of the longest  pair chain that can be formed .

You don't need to use all the pairs, you can choose some of them in any order to construct.

Problem-solving ideas:

Algorithm idea:
This question asks us to select some number pairs from the number pair array to form the longest number pair chain showing an upward trend. Look like
We select some numbers from the integer array and let these numbers form the longest ascending sequence? Therefore, we can transform the problem into i
A model we have learned: 300. The longest increasing subsequence . Therefore, our solution to the problem should be in the "longest increasing suborder
column" on this model.
However, it is different from an integer array. Before using dynamic programming to solve the problem, you should sort the array first. Because we are planning
When calculating dp[i] , you need to know all left interval ratios pairs[i] is a chain pair with a small left interval. After sorting, just use
Just "traverse forward once".
1. Status display:
dp[i] means that when the number pair ending with i is the longest Number of pairs of chain length.
2. State transfer process:
对于 dp[i] ,对历owned [0, i - 1] pairs [j]Display below, display full j District number
[1] < pairs[i][0] 's j . The most important thing to do is to raise money dp[j] , and then increase 1 、Since i Position
The longest pair chain at the end.
3. Initialization:
At the beginning, everything is initialized to 1 .
4. Filling order:
According to the "State Transition Equation", the order of filling out the form should be "from left to right".
5. Reply:
According to the "status representation", return the maximum value in the entire dp table.

Solution code:

class Solution {
public:
    int findLongestChain(vector<vector<int>>& pairs) {
        sort(pairs.begin(),pairs.end());
        int n=pairs.size();
        vector<int>dp(n,1);
        for(int i=1;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(pairs[j][1]<pairs[i][0])
                    dp[i]=max(dp[i],dp[j]+1);
            }
        }
        int ret=1;
        for(int i=0;i<n;i++)
            ret=max(ret,dp[i]);
        return ret;
    }
};

Guess you like

Origin blog.csdn.net/m0_69061857/article/details/134468115