kindly tell me why in the compare function it is necessary to sort using the second of the pair and why it doesn’t work with the firsts!
i.e, a.first < b.first is wrong
Activity Selection Problem
Second is the finish time, so we are sorting according to it. If we sort according to first we would encounter problem, eg:
10 20
11 12
13 14
15 16
as you can see sorting according to first we will have 10 and it take 10 unit time to finish it, but if we choose 11(1 unit time), 13 (unit time) similarly 15 we could have done 3 activities in the same time.(Also we cannot simply just check the next activity because we are not sure till when this kind of pattern is (I will make program n^2))
Instead on sorting acc to second we have
11 12
13 14
15 16
10 20
see simply pick activities from start till time runs out(we will get maximum result)
@akankshaanand99 if this resolves your doubt mark it as resolved.
Thank you for clearing the doubt!
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.