Longest chain of pairs
Standard · Write code · about 15 min
Each pair [a, b] has a < b. Pair [c, d] can follow [a, b] when b < c. Return the length of the longest chain you can build. You may use the pairs in any order and may leave some out.
Example 1
Input:
[[[1,2],[2,3],[3,4]]]
Output:
2
[1, 2] then [3, 4]. [2, 3] does not follow [1, 2] because 2 < 2 is false.
Example 2
Input:
[[[1,2],[7,8],[4,5]]]
Output:
3
Order them as [1, 2], [4, 5], [7, 8].
Constraints
- 1 ≤ pairs.length ≤ 1000
- −1000 ≤ a < b ≤ 1000
Learn the idea first: Longest increasing subsequence; the n log n version, a visual lesson.
More Longest increasing subsequence; the n log n version problems
- Follow the tails list Warm-up
- Longest increasing subsequence Standard
- Number of longest increasing subsequences Standard
- Russian doll envelopes Challenge