-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_4.cpp
More file actions
43 lines (37 loc) · 755 Bytes
/
question_4.cpp
File metadata and controls
43 lines (37 loc) · 755 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
42
43
#include <iostream>
#include <algorithm>
using namespace std;
struct val {
int first;
int second;
};
class Solution {
public:
int maxChainLen(struct val p[], int n) {
sort(p, p + n, [](val a, val b) {
return a.second < b.second;
});
int curr = INT_MIN;
int ans = 0;
for (int i = 0; i < n; i++) {
if (curr < p[i].first) {
curr = p[i].second;
ans++;
}
}
return ans;
}
};
int main() {
int n = 5;
val p[n] = {
{5, 24},
{39, 60},
{15, 28},
{27, 40},
{50, 90}
};
Solution ob;
cout << "Max chain length: " << ob.maxChainLen(p, n) << endl;
return 0;
}