---
# System prepended metadata

title: 0646. Maximum Length of Pair Chain
tags: [Leetcode, Intervals, Medium, Greedy]

---

# 0646. Maximum Length of Pair Chain
###### tags: `Leetcode` `Medium` `Greedy` `Intervals`
Link: https://leetcode.com/problems/maximum-length-of-pair-chain/description/
## 思路
和[0435. Non-overlapping Intervals](https://hackmd.io/uFsAyMtgTiGM9LE-8ycrqA)几乎一样
## Code
```java=
class Solution {
    public int findLongestChain(int[][] pairs) {
        Arrays.sort(pairs, (a,b)->(a[1]-b[1]));
        int idx = 1;
        int cnt = 0;
        int end = pairs[0][1];
        int n = pairs.length;
        while(idx<n){
            while(idx<n && end>=pairs[idx][0]){
                idx++;
                cnt++;
            }
            if(idx!=n) end = pairs[idx][1];
            idx++;
        }
        return n-cnt;
    }
}
```