![]() |
VOOZH | about |
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. Given the Entry(Entry[]) and Exit (Exit[]) times of individuals at a place.
Note: Entries in the register are not in sorted order.
Examples:
Input: Entry[] = [1, 2, 9, 5, 5]
Exit[] = [4, 5, 12, 9, 12]
Output: 3 5
Explanation:
Time Event Type Total Guests Present
------------------------------------------------
1 Entry 1
2 Entry 2
4 Exit 1
5 Entry 2
5 Entry 3 // Max Guests
5 Exit 2
9 Exit 1
10 Entry 2
12 Exit 1
12 Exit 0
The maximum number of guests present at any time is 3, occurring at time 5.Input: Entry[] = [3, 0, 5, 8, 6]
Exit[] = [7, 6, 9, 10, 8]
Output: 3 6
Explanation: Maximum guests are 3 at time 6Input: Entry[] = [2, 3, 5, 7, 8]
Exit[] = [4, 6, 8, 9, 10]
Output: 2 5
Explanation: Maximum guests are 2 at time 5
Table of Content
Find the minimum and maximum times from the guest entry and exit times, then iterate through this range to count the number of guests present at each time. The maximum count gives the peak number of guests.
3 5
Time Complexity: O((max-min+1)*n) where n is the length of the array of guests
Auxiliary Space: O(max-min+1) where min is the minimum time at which any guests enter and max is the maximum time at which any guests exit.
Sort the entry and exit times, then use two pointers to track guests arriving and leaving. Maintain a count of guests present at any time and update the maximum count whenever needed.
3 5
Use a difference array to record guest entries and exits, then compute a running sum to track the number of guests at any time, updating the maximum count accordingly.
3 5