c++ solution


  • 0
    Q
    class Solution {
    public:
        int findLongestChain(vector<vector<int>>& pairs) {
            struct compare{
                bool operator()(pair<int,int> a, pair<int,int> b){
                    if(a.second < b.second || (a.second == b.second && a.first>b.first))
                       return true;
                    return false;
                }
            };
            
            vector<pair<int,int>> vec;
            for(int i=0;i<pairs.size();i++){
                vec.push_back(make_pair(pairs[i][0],pairs[i][1]));
            }
            sort(vec.begin(),vec.end(),compare());
            
            int last = vec[0].second;
            
            int res = 1;
            for(int i=1;i<vec.size();i++){
                if(vec[i].first > last){
                    last = vec[i].second;
                    res++;
                }
            }
            return res;
        }
    };
    

Log in to reply
 

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