> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Length of Pair Chain Python Solution

> Tested Python solution for LeetCode 646 with 11 pytest cases. Generate a practice environment with lcpy.

LeetCode 646, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting), Longest Increasing Subsequence. [View on LeetCode](https://leetcode.com/problems/maximum-length-of-pair-chain/description/).

Generate this problem as a practice environment: tested reference solution, 11 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 646   # by problem number
lcpy gen -s maximum_length_of_pair_chain   # by problem name
```

## Problem

You are given an array of `n` pairs `pairs` where `pairs[i] = [lefti, righti]` and `lefti < righti`.

A pair `p2 = [c, d]` **follows** a pair `p1 = [a, b]` if `b < c`. A **chain** of pairs can be formed in this fashion.

Return *the length longest chain which can be formed*.

You do not need to use up all the given intervals. You can select pairs in any order.

### Examples

```
Input: pairs = [[1,2],[2,3],[3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4].
```

```
Input: pairs = [[1,2],[7,8],[4,5]]
Output: 3
Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
```

### Constraints

* n == pairs.length
* 1 \<= n \<= 1000
* -1000 \<= lefti \< righti \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_length_of_pair_chain/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_length_of_pair_chain/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n) for the sorted copy
    def find_longest_chain(self, pairs: list[list[int]]) -> int:
        count = 0
        chain_end = float("-inf")
        for left, right in sorted(pairs, key=lambda pair: pair[1]):
            if left > chain_end:
                count += 1
                chain_end = right
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) for the sorted copy |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.