C++, O(nlgn) time, O(1) space


  • 0
    N
    class Solution {
    public:
        int findLongestChain(vector<vector<int>>& pairs) {
            sort(pairs.begin(), pairs.end(), [](vector<int>& a, vector<int>& b){
                return a[1] < b[1];
            });
            
            int res = 1;
            int prev = pairs[0][1];
            for(int i = 1; i < pairs.size(); i++){
                if(pairs[i][0] > prev){
                    res++;
                    prev = pairs[i][1];
                }
            }
            
            return res;
        }
    };
    

Log in to reply
 

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