-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path646.maximum-length-of-pair-chain.cpp
63 lines (63 loc) · 1.44 KB
/
646.maximum-length-of-pair-chain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode id=646 lang=cpp
*
* [646] Maximum Length of Pair Chain
*
* https://leetcode.com/problems/maximum-length-of-pair-chain/description/
*
* algorithms
* Medium (47.37%)
* Total Accepted: 29.2K
* Total Submissions: 61.7K
* Testcase Example: '[[1,2], [2,3], [3,4]]'
*
*
* You are given n pairs of numbers. In every pair, the first number is always
* smaller than the second number.
*
*
*
* Now, we define a pair (c, d) can follow another pair (a, b) if and only if b
* < c. Chain of pairs can be formed in this fashion.
*
*
*
* Given a set of pairs, find the length longest chain which can be formed. You
* needn't use up all the given pairs. You can select pairs in any order.
*
*
*
* Example 1:
*
* Input: [[1,2], [2,3], [3,4]]
* Output: 2
* Explanation: The longest chain is [1,2] -> [3,4]
*
*
*
* Note:
*
* The number of given pairs will be in the range [1, 1000].
*
*
*/
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
sort(pairs.begin(), pairs.end(), [](vector<int>& a, vector<int>& b){ return a[1]<b[1]; });
// for(auto v: pairs){
// for(auto e: v)
// cout<<e<<" ";
// cout<<endl;
// }
int end = INT_MIN;
int count = 0;
for(int i=0;i<(int)pairs.size();i++){
if(end < pairs[i][0]){
count++;
end = pairs[i][1];
}
}
return count;
}
};