-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activity Selection Problem.cpp
60 lines (55 loc) · 1.4 KB
/
Activity Selection Problem.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
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to find the maximum number of activities that can
//be performed by a single person.
static bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){
return (a.second < b.second);
}
int activitySelection(vector<int> start, vector<int> end, int n)
{
// Your code here
vector< pair<int,int> > v;
for(int i=0;i<n;i++){
v.push_back(make_pair(start[i],end[i]));
}
sort(v.begin(), v.end(), sortbysec);
int finish=v[0].second;
int count=1;
for(int i=1;i<n;i++){
if(v[i].first>finish){
count+=1;
finish=v[i].second;
}
}
return count;
}
};
// { Driver Code Starts.
int main()
{
int t;
//testcases
cin >> t;
while(t--)
{
//size of array
int n;
cin >> n;
vector<int> start(n), end(n);
//adding elements to arrays start and end
for(int i=0;i<n;i++)
cin>>start[i];
for(int i=0;i<n;i++)
cin>>end[i];
Solution ob;
//function call
cout << ob.activitySelection(start, end, n) << endl;
}
return 0;
}
// } Driver Code Ends