Activity Selection Problem

how to solve this problem

Sort the activities in the increasing order of ending times and then do a single traversal and pick up activities greedily, i.e, choose an activity whenever possible.

pick up activities greedily means

After sorting as I mentioned. Take a time counter , let’s say t. Set it to 0 . It tells up to what point are you busy.
Now we pick the first activity because its start time will obviously be greater than or equal to 0. Now set t equal to end time of this first activity. After this traverse the array and find next suitable activity, i.e., activity whose start time>=t , and then set t equal to end time of that activity, keep doing it until you traverse till the end of the array and keep a count of how many activities you have done.

1 Like

I written this code as you said but one test case is failing.

Yeah just use endl while printing co
cout<<co<<endl;

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.