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 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 count of active bookings at any moment is more than k, then return false. Else return return true.
Below is the c++ code for above approach.
Time Complexity : O(n Log n)
Auxiliary Space : O(n)
- Find the only element that appears b times
- Given an array of of size n and a number k, find all elements that appear more than n/k times
- Find the Number Occurring Odd Number of Times
- Sum of all elements repeating 'k' times in an array
- Remove elements from the array which appear more than k times
- Remove elements that appear strictly less than k times
- First element occurring k times in an array
- Sum of array elements after every element x is XORed itself x times
- Largest element in the array that is repeated exactly k times
- First element that appears even number of times in an array
- Smallest element in an array that is repeated exactly 'k' times.
- Two odd occurring elements in an array where all other occur even times
- Element which occurs consecutively in a given subarray more than or equal to K times
- Lexicographically largest subsequence such that every character occurs at least k times
- Unique element in an array where all elements occur k times except one
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.