Simple and short C++ DP solution

  • 0
        int findLongestChain(vector<vector<int>>& pairs) {
            sort(pairs.begin(), pairs.end(), comp);
            vector<int> dp(pairs.size(), 0);
            int res=0;
            for(int i=0;i<pairs.size();i++) {
                for(int j=0;j<i;j++) {
                    if(pairs[i][0]>pairs[j][1]) dp[i]=max(dp[i], dp[j]);
                res=max(res, dp[i]);
            return res;
        static bool comp(vector<int>& a, vector<int>& b) {
            return a[1]<b[1];

Log in to reply

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