activity selection problem

Solutions on MaxInterview for activity selection problem by the best coders in the world

showing results for - "activity selection problem"
Judith
08 Apr 2020
1// Greedy appraoch TC:O(nlogn)
2#include<bits/stdc++.h>
3using namespace std;
4struct activity
5{
6    int start,end;
7};
8bool sort_a(activity s1,activity s2)
9{
10    return(s1.end<s2.end);
11}
12void max_activity(activity arr[],int n)
13{
14    sort(arr,arr+n,sort_a);
15    int i=0;
16    cout<<"("<<arr[i].start<<","<<arr[i].end<<")"<<endl;
17    for(int j=1;j<n;j++)
18    {
19        if(arr[j].start>=arr[i].end)
20        {
21          cout<<"("<<arr[j].start<<","<<arr[j].end<<")"<<endl;
22          i=j;
23        }
24    }
25}
26int main()
27{
28    int n;
29    cin>>n;
30    activity arr[n];
31    for(int i=0;i<n;i++)
32    {
33      cin>>arr[i].start>>arr[i].end;
34    }
35    max_activity(arr,n);
36    return 0;
37}
38
similar questions
queries leading to this page
activity selection problem