-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-problem.cpp
More file actions
41 lines (31 loc) · 797 Bytes
/
Copy path04-problem.cpp
File metadata and controls
41 lines (31 loc) · 797 Bytes
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
#include<bits/stdc++.h>
using namespace std;
struct Pair {
int a;
int b;
};
bool comparison(Pair p1, Pair p2) {
return p1.b < p2.b;
}
int maxChainLen(Pair arr[], int n) {
sort(arr, arr+n, comparison);
int count = 1;
int last = arr[0].b;
for(int i=1; i<n; i++) {
if(arr[i].a > last) {
last = arr[i].b;
count++;
}
}
return count;
}
int main() {
Pair arr[] = {{5,24}, {39,60}, {15,28}, {27,40}, {50,90}};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Example 1 output: " << maxChainLen(arr, n) << endl;
cout << endl;
Pair arr2[] = {{5,10}, {1,11}};
int m = sizeof(arr2)/sizeof(arr2[0]);
cout << "Example 2 output: " << maxChainLen(arr2, m) << endl;
return 0;
}