Ninja is the steel factory owner, and there are 'N' workers working at that factory. Each worker has his own working time, and it is provided in the array 'intervals' where 'INTERVALS[i][0]' gives the start time of the 'i'th worker and 'INTERVALS[i][1]' gives the end time of the 'i'th worker.
Ninja does not want to allow more than one worker to work at the same time, so he needs your help to find the minimum number of workers he needs to reschedule their work time so as there are non-overlapping working times in any two workers.
Example:Input: 'INTERVALS' = [[1, 2], [1, 3], [2, 3], [3, 4]]
Output: 1
After rescheduling the worker with the interval [1, 3] it is possible to get the non-overlapping working times.
The first line of input contains an integer 'T', denoting the number of test cases.
The first line contains an integer 'N' size of the list 'intervals' next 'N' lines will contain two integers, each start and end time for each worker.
Output format :
For each test case, print the minimum number of workers needed to reschedule their work time so as there are non-overlapping working times in any two workers.
Output for each test case will be printed in a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= 'N' <= 10^4
'INTERVALS[i].LENGTH' == 2
'INTERVALS[i][0]' < 'INTERVALS[i][1]'
'INTERVALS[i][j]' <= 10^9
Time Limit: 1sec
2
4
1 2
2 3
1 3
3 4
3
3 4
4 5
5 6
1
0
For the first test case, After rescheduling the worker with the interval [1, 3] it is possible to get the non-overlapping working times.
For the second test case, there are no overlapping time intervals. Hence we do not need to reschedule any worker's time.
2
5
2 3
4 5
6 7
2 4
3 6
4
4 6
4 7
3 5
3 5
2
3
Can you approach according to the ending point of each interval?
Approach: For this problem, we just want to eliminate some intervals so there are no overlapping intervals after that.
We can easily achieve this by sorting the intervals by their ending position and then greedily incrementing the counter according to the number of overlaps.
Algorithm :
O(N * LogN), Where 'N' is the size of the input array 'INTERVALS'.
As we are sorting the array of size 'N' and then iterating over time, complexity will be O(N* LogN).
O(1)
As we are using the constant extra space, the space complexity will be O(1).