Hotel Bookings Possible
Problem Description
A hotel manager has to process N advance bookings of rooms for the next season. His hotel has C 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. Write a program that solves this problem in time O(N log N) .
Problem Constraints
|A|== |B|
1 <= Ai <= Bi <= 108
1 <= C <= 106
Input Format
First argument is an integer array A containing arrival time of booking.
Second argument is an integer array B containing departure time of booking.
Third argument is an integer C denoting the count of rooms.
Output Format
Return True if there are enough rooms for N bookings else return False.
Return 0/1 for C programs.
Example Input
Input 1:
A = [1, 3, 5] B = [2, 6, 8] C = 1
Input 2:
A = [1, 2, 3] B = [2, 3, 4] C = 2
Example Output
Output 1:
0
Output 2:
1
Example Explanation
Explanation 1:
At day = 5, there are 2 guests in the hotel. But I have only one room.
Explanation 2:
At day = 1, there is 1 guest in the hotel.
At day = 2, there are 2 guests in the hotel.
At day = 3, there are 2 guests in the hotel.
At day = 4, there is 1 guest in the hotel.
We have two rooms available, which satisfy the demand.
bool Solution::hotel(vector<int> &arrive, vector<int> &depart, int K) {vector<pair<int,int>>v;for(int i=0; i<arrive.size(); i++){v.push_back({arrive[i],1});v.push_back({depart[i]+1,-1});}int cnt=0;sort(v.begin(),v.end());for(int i=0; i<v.size(); i++){cnt+=v[i].second;if(cnt>K)return false;}return true;}
0 Comments
If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.