Maximum Length of Pair Chain

MediumGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given n pairs where pairs[i] = [left, right] and left < right. Pair [c, d] can follow pair [a, b] in a chain when b < c (strictly). Pairs may be used in any order and each at most once. Return the length of the longest chain you can build.

Examples

Input: pairs = [[1,2],[7,8],[2,3],[4,5]]
Output: 3
One longest chain is [1,2] -> [4,5] -> [7,8]. [1,2] cannot be followed by [2,3] because 2 < 2 is false.
Input: pairs = [[1,5],[2,3],[3,4]]
Output: 1
Every two pairs overlap or touch, so no chain is longer than one pair.

Constraints

  • 1 <= n <= 1000
  • -1000 <= left < right <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems