
If ‘N’ = 3 and ‘K’ = 2, and the [‘startTime’ , ‘endTime’] of the 3 rides are [0,2],[1,3],[2,4]
Then the 2 friends can attend all the 3 rides as follows:
Friend 1 will attend ride [0,2] at t = 0
Friend 2 will attend ride [1,3] at t = 1
Friend 1 will be free at t = 2, and will now attend ride [2,4] at t = 2
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then ‘T’ test cases follow:
The first line of each test case has 2 space-separated integers ‘N’ and ‘K’, where ‘N’ is the number of rides given and ‘K’ is the number of friends.
Following ‘N’ lines of each test case have 2 space-separated integers, ‘startTime’ and ‘endTime’ of each ride.
For each test case, print an integer denoting the maximum number of rides the Ninja's friends can attend.
Output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= K <= N <= 10 ^ 5
0 <= startTime <= endTime <= 10 ^ 9
Where ‘startTime’ is the starting time of a ride and ‘endTime’ is the ending time of the ride.
Time limit: 1 sec
The idea is to first sort the rides in ascending order according to their ‘endTime’. This is done so that when a friend wants to attend a ride, he’s given the ride which will end the earliest so that he is free at the earliest time.
Then, for each ride, we will try to find a friend who is available to attend that ride and was freed at the latest so that other friends who were freed before him are available to attend later ride (which may have a starting time lesser than the current ride).
carnivalRides(‘N’, ‘K’, ‘rides’):
Here, ‘N’ is the number of rides available, ‘K’ is the number of friends and ‘rides’ is the array/list that stores ‘startTime’ and ‘endTime’ of all the ‘N’ rides, so ‘rides[i]’ = [‘start time of ride i’ , ‘end time of ride i’].
Here is the algorithms: