c++ dp solution


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

Log in to reply
 

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