LeetCode_Add Maximum Length of Pair Chain Problem Solution (C++)

Keywords: Programming less

stem

The original website:
https://leetcode.com/problems/maximum-length-of-pair-chain/description/

Analysis of the Topics

Give you n pairs of numbers. The first number of each pair is always less than the second number. Define a rule for pairs of numbers, assuming that we have (c, d) and (a, b) pairs of numbers if and only if B

Knowledge Points

dynamic programming

difficulty

secondary

Thoughts on Problem Solving

This problem is a common dynamic programming problem. The idea is to sort these pairs of numbers first and arrange them from small to large according to the size of the first number in each pair. Then traverse back and forth from the beginning, there are only two cases for each pair, either selected or not selected. If selected, the length of the chain will be increased by one, but the next starting pair will be limited by the second data of the pair, because the first data of the next pair is larger than the second data of the previous pair; if not selected, it will start from the next pair. Simply compare the selected with the unselected to get the longest chain. The specific code is as follows.

Code

int *long_num;  // Used to preserve the calculated value of the longest subsequence from the subscript j to prevent repeated calculation
class Solution {
public:
    int findlong(vector<vector<int> >& pairs, int j) {  // Find the longest subsequence in pairs starting with the subscript j
        if (j >= pairs.size()) {  // If it has crossed the boundary, return 0
            return 0;
        } else if(*(long_num + j) != -1) {  // If this sequence has been computed, return the previous value directly.
            return *(long_num + j);
        } else {
            int yes, no;  // yes means you want to add the j pair to the sequence, no means you don't.
            no = findlong(pairs, j + 1);  // If not, the longest subsequence starting from the next subscript is directly obtained.
            int k = j + 1;  // If you want to add, the first element of the next subscript pair should be larger than the second element of the current pair.
            while (k < pairs.size() && pairs[k][0] <= pairs[j][1]) {
                k++;
            }
            if (k >= pairs.size()) {  // If you don't find the right next pair, you've crossed the line.
                yes = 1;
            } else {
                yes = 1 + findlong(pairs, k);
            }
            if (yes > no) {  // If the longest subsequence is greater than that without addition
                *(long_num + j) = yes;
                return yes;
            } else {   // Conversely
                *(long_num + j) = no;
                return no;
            }
        }
    }
    int findLongestChain(vector<vector<int> >& pairs) {
        for (int i = pairs.size() - 1; i > 0; i--) {  // Ordering each pair according to the first element of each pair 
            for (int j = 0; j < i; j++) {
                if (pairs[j][0] > pairs[j + 1][0]) {
                    vector<int> temp = pairs[j];
                    pairs[j] = pairs[j + 1];
                    pairs[j + 1] = temp;
                }
            }
        }
        long_num = new int[pairs.size()];
        for (int i = 0; i < pairs.size(); i++) {  // long_num is all initialized to -1
            *(long_num + i) = -1;
        }
        return findlong(pairs, 0);
    }
};

Posted by miracle_potential on Thu, 07 Feb 2019 07:06:17 -0800