Maximum Length of Pair Chain

Medium Topics Company Tags

Each entry of pairs is [left, right] with left < right. A pair can follow another only when its left endpoint is strictly greater than the previous pair's right endpoint. Select any subset of pairs and arrange them in any order to form a chain. Return the largest possible number of pairs in a chain. Equal touching endpoints cannot be joined.

Example 1:

Input: pairs = [[5,6],[-2,0],[2,3]]

Output: 3

Explanation: Reorder the pairs as [-2,0], [2,3], [5,6]. Since 0 < 2 and 3 < 5, all three can be used.

Example 2:

Input: pairs = [[1,3],[3,5]]

Output: 1

Explanation: The endpoint 3 is shared. The required inequality is strict, so the two pairs cannot be chained and the answer is 1.

Example 3:

Input: pairs = [[0,10],[1,2],[4,5],[7,8]]

Output: 3

Explanation: Use [1,2], [4,5], [7,8] for a chain of length 3. The wide pair [0,10] cannot be combined with any of them.

Constraints:

  • 1 <= pairs.length <= 1000
  • Every pair contains exactly two integers.
  • -1000 <= left < right <= 1000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.