5-liner O(N*logN) time O(1) space

  • 0
        int findLongestChain(vector<vector<int>>& pairs) {
            sort(pairs.begin(), pairs.end(), [](vector<int>& a, vector<int>& b){ return a[1] < b[1]; });
            int res = 0, curEnd = INT_MIN;
            for (auto& p : pairs)
                if (curEnd < p[0]) curEnd = p[1], ++res;
            return res;

Log in to reply

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