A hotel manager has to process N advance bookings of rooms for the next season. His hotel has K rooms. Bookings contain an arrival date and a departure date. He wants to find out whether there are enough rooms in the hotel to satisfy the demand.
The idea is to sort the arrays and keep track of overlaps.
Input : Arrivals : [1 3 5] Departures : [2 6 8] K: 1 Output: False Hotel manager needs at least two rooms as the second and third intervals overlap.
The idea is store arrival and departure times in an auxiliary array with an additional marker to indicate whether the time is arrival or departure. Now sort the array. Process the sorted array, for every arrival increment active bookings. And for every departure, decrement. Keep track of maximum active bookings. If the count of active bookings at any moment is more than k, then return false. Else return true.
Below is the implementation of the above approach:
Time Complexity: O(n Log n)
Auxiliary Space: O(n)
The idea is to simply sort the 2 arrays (Array for arrival dates and Array for departure dates) first.
Now, the next step would be to check how many overlaps are present in one particular range. If the number of overlaps are greater than the number of rooms, we can say that we have less rooms to accommodate guests.
So, for a particular range where arrival date(ith of Arrival array) being the start date and departure date(ith of departure array) being the end date, overlap can be only possible if the next arrival dates(from i+1th) are less than end date of the range and greater than or equal to start date of the range (Since this is a sorted array, we don’t need to take care about the latter condition).
Considering the fact, that we have sorted array, we directly need to check if the next Kth (i+Kth) arrival date falls in the range, if it does, all the dates before that arrival date will also fall in the taken range, resulting in K+1 overlaps with the range in question, hence exceeding the number of rooms.
Following is the Implementation of above Approach –
Time Complexity: O(n Log n)
Auxiliary Space: O(n) used by Python sort
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Count different numbers possible using all the digits their frequency times
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Queries to increment array elements in a given range by a given value for a given number of times
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Find the number of different numbers in the array after applying the given operation q times
- Find the maximum difference after applying the given operations two times on a number
- Check if given array can be made 0 with given operations performed any number of times
- Minimum LCM and GCD possible among all possible sub-arrays
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Find the Number Occurring Odd Number of Times
- Find the only element that appears b times
- C/C++ Program to Find the Number Occurring Odd Number of Times
- Find the K-th minimum element from an array concatenated M times
- Find Nth smallest number that is divisible by 100 exactly K times
- Find original sequence from Array containing the sequence merged many times in order
- Find the elements appearing even number of times in an Array
- Find all array elements occurring more than ⌊N/3⌋ times
- Element which occurs consecutively in a given subarray more than or equal to K times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.