Description Link to heading

646. Maximum Length of Pair Chain (Medium)

You are given an array of n pairs pairs where pairs[i] = [leftᵢ, rightᵢ] and leftᵢ < rightᵢ. A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion. Return the length longest chain which can be formed. You do not need to use up all the given intervals. You can select pairs in any order. Example 1:

Input: pairs = [[1,2],[2,3],[3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4].

Example 2:

Input: pairs = [[1,2],[7,8],[4,5]]
Output: 3
Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].

Constraints:

  • n == pairs.length
  • 1 <= n <= 1000
  • -1000 <= leftᵢ < rightᵢ <= 1000

Solution Link to heading

Greedy Algorithm Link to heading

When we choose the first pair, we need to choose i which makes pairs[i][1] is the minimum. When we choose the second pair, we need to choose j, which makes pairs[j][0] > pairs[i][1] and pairs[j][1] is the minimum. And so on…

So we need sort pairs by pairs[i][1].

Code Link to heading

class Solution {
  public:
    int findLongestChain(vector<vector<int>> &pairs) {
        auto cmp = [&](vector<int> &v1, vector<int> &v2) {
            if (v1[1] == v2[1]) {
                return v1[0] <= v2[0];
            }
            return v1[1] < v2[1];
        };
        std::sort(pairs.begin(), pairs.end(), cmp);
        int cnt = 1;
        int left = pairs[0][1];
        for (int i = 1; i < pairs.size(); i++) {
            if (pairs[i][0] > left) {
                cnt++;
                left = pairs[i][1];
            }
        }
        return cnt;
    }
};