2Brute2Best
🔥0

Maximum Length of Pair Chain

Medium·LeetCode #646 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You get n pairs [left, right]. In each pair, left < right.

A pair [c, d] can follow a pair [a, b] in a chain when b < c. Build the longest chain you can.

You may use the pairs in any order, and you don't have to use all of them. Return the length of the longest chain.

Input

An array pairs of n pairs, where pairs[i] = [left_i, right_i] and left_i < right_i.

Output

One integer: the length of the longest chain.

Example 1
Input: pairs = [[1,2], [2,3], [3,4]]
Output: 2

The chain is [1,2] → [3,4], because 2 < 3. The pair [2,3] can't follow [1,2], since 2 is not less than 2.

Example 2
Input: pairs = [[1,2], [7,8], [4,5]]
Output: 3

Since the order is free, the chain is [1,2] → [4,5] → [7,8].

Constraints
  • • 1 ≤ n ≤ 1000, where n = pairs.length
  • • −1000 ≤ left_i < right_i ≤ 1000
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“longest chain”
→ Maximise the count of pairs that can follow one another.
“b < c”
→ Each pair must end strictly before the next one starts. The check is strict.
“any order”
→ We must choose the order ourselves. Sorting is the first step.
03

Approach Complexity Ladder

Brute Force — try every subset in sorted orderO(2^N · N log N)O(N)

Try every subset of pairs; sort each one and check that it forms a chain

Better — LIS-style DPO(N²)O(N)

dp[i] = longest chain ending at pair i, found by checking every earlier pair

Optimal — sort by right, take what fitsO(N log N)O(N)

Sort by the right number; take a pair when its left is above the last right taken

Brute Force Intuition

Go through all 2^n subsets of pairs. For each subset, sort it by the left number and check that each pair ends strictly before the next one starts. Keep the largest subset that passes.

Recognition rule for this sub-pattern:

To fit the most activities, always take the one that finishes earliest.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.