Answers for "activity selection problem"

C++
0

activity selection problem

// Greedy appraoch TC:O(nlogn)
#include<bits/stdc++.h>
using namespace std;
struct activity
{
    int start,end;
};
bool sort_a(activity s1,activity s2)
{
    return(s1.end<s2.end);
}
void max_activity(activity arr[],int n)
{
    sort(arr,arr+n,sort_a);
    int i=0;
    cout<<"("<<arr[i].start<<","<<arr[i].end<<")"<<endl;
    for(int j=1;j<n;j++)
    {
        if(arr[j].start>=arr[i].end)
        {
          cout<<"("<<arr[j].start<<","<<arr[j].end<<")"<<endl;
          i=j;
        }
    }
}
int main()
{
    int n;
    cin>>n;
    activity arr[n];
    for(int i=0;i<n;i++)
    {
      cin>>arr[i].start>>arr[i].end;
    }
    max_activity(arr,n);
    return 0;
}
Posted by: Guest on July-17-2021

Browse Popular Code Answers by Language