<p>You are given an array of <code>n</code> pairs <code>pairs</code> where <code>pairs[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> and <code>left<sub>i</sub> &lt; right<sub>i</sub></code>.</p>

<p>A pair <code>p2 = [c, d]</code> <strong>follows</strong> a pair <code>p1 = [a, b]</code> if <code>b &lt; c</code>. A <strong>chain</strong> of pairs can be formed in this fashion.</p>

<p>Return <em>the length longest chain which can be formed</em>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> pairs = [[1,2],[2,3],[3,4]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest chain is [1,2] -&gt; [3,4].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> pairs = [[1,2],[7,8],[4,5]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest chain is [1,2] -&gt; [4,5] -&gt; [7,8].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == pairs.length</code></li>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
	<li><code>-1000 &lt;= left<sub>i</sub> &lt; right<sub>i</sub> &lt;= 1000</code></li>
</ul>


class rightCompare {
public:
    bool operator()(const vector<int>& a, const vector<int>& b) const {
        return a[1] < b[1];
    }
};

class Solution {
public:
    static bool comp(pair<int,int>a,pair<int,int>b){
        
        return a.second < b.second;
    }
    int findLongestChain(vector<vector<int>>& pairs) {
        cin.tie(0);
        cout.tie(0);
        ios_base::sync_with_stdio(false);
        vector<pair<int,int>>v;
        for(int i = 0; i < pairs.size(); i++){
            v.push_back({pairs[i][0], pairs[i][1]});
        }
        sort(v.begin(), v.end(), comp);
        
        int ans = 1;
        int r = v[0].second;
        
        for(int i = 1; i < v.size(); i++){
            if(v[i].first > r){
                r = v[i].second;
                ans++;
            }
        }
        
        return ans;
        
    }
};
28670