Skip to content
Decoder

Maximum Length of Pair Chain

Medium

Problem

Each pair is [left, right] with left < right. Pair [c, d] can follow pair [a, b] when b < c, strictly. Build the longest chain of pairs where each one follows the previous. You may pick pairs in any order and skip any you like. Return the chain's length.

Example 1

Input
[[1, 2], [2, 3], [3, 4]]
Output
2
Why
[1, 2] then [3, 4]

Example 2

Input
[[1, 2], [7, 8], [4, 5]]
Output
3
Why
[1, 2] then [4, 5] then [7, 8]

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.