Problem · Array
Maximum Length Pair Chain
Learn this problemProblem statement
You are given an array pairs, where every pair is [start, end] and start < end.
A pair [c, d] may follow [a, b] in a chain only when b < c. You may choose each pair at most once, may leave pairs unused, and may arrange chosen pairs in any order.
Return the maximum possible number of pairs in a valid chain.
Function
maxPairChainLength(pairs: int[][]) → intExamples
Example 1
pairs = [[3,4],[1,2],[2,3]]return = 2One maximum chain is [1,2] -> [3,4]. The pair [2,3] cannot immediately follow [1,2] because the inequality is strict.
Example 2
pairs = [[-10,-5],[-4,0],[1,2]]return = 3All three pairs form a valid chain in their displayed order.
Constraints
1 <= pairs.length <= 1000- Every
pairs[i]has exactly two integers. -10^9 <= pairs[i][0] < pairs[i][1] <= 10^9