There is number of elephants time span given, here time span means, year of birth to year of death.
You have to calculate the period where maximum number of elephants are alive.
OR
Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any order.
OR
At a bus-station, you have time-table for buses arrival and departure. You need to find the minimum number of platforms so that all the buses can be accommodated as per their schedule.
OR
Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required.
Input: intervals =[[1,4],[2,5],[9,12],[5,9],[5,12]]Output: 3Explanation: First guest in array arrives at 1 and leaves at 4,second guest arrives at 2 and leaves at 5, and so on.There are maximum 3 guests at time 5.
Example 2 - Elephant with start and end date of there lives
1
2
3
Input: intervals =[[1990,2013],[1995,2000],[2010,2020],[1992,1999]]Output: 3Explanation: There are 3 elephants living in period [1995,1999].
Example 3 - Number of bus stations or bus station platform
Inspect each time interval to determine the earliest and latest times (when the first and last guest respectively arrive and depart).
Construct an array titled count[] with a length equal to max – min + 1.
Within every interval [x, y], iterate from i = x to y and execute count[i – min]++ for each iteration.
Locate the count array’s largest value and note its index, known as ‘max_index’, then return the sum of max_index + min.
The memory requirement for the aforementioned method is O(max-min+1) and its temporal complexity depends on interval lengths. In the extreme case where every interval stretches from ‘min’ to ‘max,’ the complexity is O((max-min+1)*n), with n as the quantity of intervals.
Split each date range into start date and end date.
Sort the dates. If a start date and an end date are the same, put the end date first. For eg. in case of meeting rooms, we will have the meeting room free first, before someone can take it.
count =0lastStart = N/A
1990: count =1 count =1>0, so set flag
and lastStart =19901992: count =2 count =2>0, so set flag
and lastStart =19921995: count =3 count =3>0, so set flag
and lastStart =19951999: flag is set, so
record [lastStart (=1995),1999]with a count of 3 reset flag
count =22000: flag is not set
reset flag
count =12010: count =2 since count =2<3, don't set flag
2013: flag is not set
reset flag
count =12020: flag is not set
reset flag
count =0
Example 2 -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
arr[]= {1, 2, 10, 5, 5}
dep[]= {4, 5, 12, 9, 12}
Below are all events sorted by time. Note that in sorting, if two
events have same time, then arrival is preferred over exit.
Time Event Type Total Number of Guests Present
------------------------------------------------------------1 Arrival 1
2 Arrival 2
4 Exit 1
5 Arrival 2
5 Arrival 3 // Max Guests5 Exit 2
9 Exit 1
10 Arrival 2
12 Exit 1
12 Exit 0
Time complexity - O(nLogn) time.
What if the number of elephants are very large?
For a (very) large number of elephants, it might be worth skipping the sort. You could create an array indexed by year with +1 for birth, -1 for death. O(e) to fill, and O(n) to sweep, where e is number of elephants and n is date range.
Following is the implementation of above approach. Note that the implementation doesn’t create a single sorted list of all events, rather it individually sorts arr[] and dep[] arrays, and then uses merge process of merge sort to process them together as a single sorted array.
staticclassIntervalPointimplements Comparable {
int timestamp;
boolean isStart;
publicIntervalPoint(int timestamp, boolean isStart) {
this.timestamp= timestamp;
this.isStart= isStart;
}
@OverridepublicintcompareTo(Object o) {
if (o instanceof Endpoint) {
IntervalPoint other = (Endpoint) o;
int result = Integer.compare(timestamp, other.timestamp);
//in case a result is 0, then one which starts early should be returnedif (result == 0) {
return Boolean.compare(((Endpoint) o).isStart, isStart);
} else {
return result;
}
}
return 0;
}
}
We can also use minHeap to store the end intervals. We need to keep track of the ending time of all the meetings currently happening so that when we try to schedule a new meeting, we can see what meetings have already ended. We need to put this information in a data structure that can easily give us the smallest ending time. A Min Heap would fit our requirements best.
Sort all the meetings on their start time.
Create a min-heap to store all the active meetings. This min-heap will also be used to find the active meeting with the smallest end time.
Iterate through all the meetings one by one to add them in the min-heap. Let’s say we are trying to schedule the meeting m1.
Since the min-heap contains all the active meetings, so before scheduling meeting m1 we can remove all meetings from the heap that have ended before m1, i.e., remove all meetings from the heap that have an end time smaller than or equal to the start time of m1.
Now add m1 to the heap.
The heap will always have all the overlapping meetings, so we will need rooms for all of them. Keep a counter to remember the maximum size of the heap at any time which will be the minimum number of rooms needed.