C++ DP code


  • 0
    J
    auto mycompare =[](vector<int>& a, vector<int>&b){ return a[0]<b[0];};
    class Solution {
        
    public:
        int findLongestChain(vector<vector<int>>& pairs) {
            int len=pairs.size();
            if(len == 0)
                return 0;
            sort(pairs.begin(), pairs.end(), mycompare);
            
            int res=0;
            vector<int> dp(len, 1);
            dp[0]=1;
            for(int i=1; i<len; i++)
            {
                for(int j=i-1; j>=0; j--)
                {
                    if(pairs[j][1]<pairs[i][0])
                    {
                        dp[i]=max(dp[i], dp[j]+1);
                    }
                }
            }
            return dp[len-1];
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.