C++ 8 lines sort and greedy


  • 0
    B
    class Solution {
    public:
        int findLongestChain(vector<vector<int>>& pairs) {
            sort(pairs.begin(), pairs.end(), 
                 [](vector<int>&v1, vector<int>&v2)
                 {return v1[1] < v2[1] || (v1[1] == v2[1] && v1[0] > v2[0]);});
            if(pairs.empty()) return 0;
            int ans = 1;
            auto cur = pairs[0];
            for(auto e : pairs)
            {
                if(e[0] <= cur[1]) continue;
                ans++;
                cur = e;
            }
            return ans;
        }
    };

Log in to reply
 

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