Maximum Length of Pair Chain
Description:
You are given an array of n pairs pairs where pairs[i] = [left_i, right_i] and left_i < right_i.
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.
Examples:
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.length1 <= n <= 1000-1000 <= left_i < right_i <= 1000
Video Explanation:
Approaches:
1. Dynamic Programming (Recursion with Memoization - LIS Variant)
Intuition:
This problem can be framed as a variation of the classic Longest Increasing Subsequence (LIS) problem.
Since we can select pairs in any order, we should first sort the pairs in ascending order based on their first element (pairs[i][0]).
Once sorted:
- For each pair at index
indx, we have two decisions:- Take the pair: We can only take the pair if it is the first pair we pick (
prevI == -1) or if its start coordinate is strictly greater than the end coordinate of the previously chosen pair (pairs[indx][0] > pairs[prevI][1]). If taken, the chain length increases by1, and the new previous index becomesindx. - Do not take the pair: We skip the current pair and advance to the next index without modifying
prevI.
- Take the pair: We can only take the pair if it is the first pair we pick (
- The answer for the current state is the maximum of the two choices.
- To prevent recomputing overlapping subproblems, we use a 2D memoization table
dp[indx][prevI + 1]. The+1shift handles the base case whenprevI == -1.
Complexity:
- Time Complexity: where is the number of pairs. There are states, and each transition takes time. Sorting takes .
- Space Complexity: for the 2D DP memoization table and recursion stack space.
2. Greedy Approach (Optimal)
Intuition:
We can also view this problem as an Interval Scheduling Problem. To maximize the number of non-overlapping intervals (pairs), we should always choose the pair that ends earliest, leaving the maximum possible room for subsequent pairs.
- Sort
pairsin ascending order by their second element (pairs[i][1]). - Maintain
curr_endinitialized to negative infinity. - For each pair
[start, end], ifstart > curr_end, increment the chain count and updatecurr_end = end.
Complexity:
- Time Complexity: due to sorting the pairs.
- Space Complexity: auxiliary space (or depending on the sorting implementation).