clueless coding // TODO: be smarter

LeetCode Maximum Length of Pair Chain: CPP Solution



TL;DR Code Solution


int findLongestChain(vector<vector<int>>& pairs) {

    sort(pairs.begin(), pairs.end(), [](vector<int>& a, vector<int>& b) {
        return a[1] < b[1] || (a[1] == b[1] && a[0] < b[0]);    
    });

    if (pairs.size() <= 1) {
        return pairs.size();
    }

    int currentEnd = pairs[0][1];
    int res = 1;
    for (int i = 1; i < pairs.size(); i++) {
        if (pairs[i][0] > currentEnd) {
            res++;
            currentEnd = pairs[i][1];
        }

    }


    return res;

}