-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotel_booking_possible.cpp
More file actions
46 lines (38 loc) · 970 Bytes
/
hotel_booking_possible.cpp
File metadata and controls
46 lines (38 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int no_of_rooms_needed(vector<int> &arrival, vector<int> &departure){
int size=arrival.size();
int a_idx=0,d_idx=0;
sort(arrival.begin(),arrival.end());
sort(departure.begin(), departure.end());
int max_so_far=0, curr=0;
while(a_idx<size){
if(arrival[a_idx]<departure[d_idx]){
a_idx++;
curr++; //someone needs a room
max_so_far=max(curr,max_so_far);
}
else{
d_idx++;
curr--; //someone vacates a room
}
}
return max_so_far;
}
bool booking_possible(vector<int> &arrival, vector<int> &departure, int rooms_available){
return rooms_available >= no_of_rooms_needed(arrival, departure);
}
int main(){
vector<int> arrival={1,3,5};
vector<int> departure={2,6,8};
int rooms_available=1;
if(booking_possible(arrival, departure, rooms_available)){
cout<<"hotel booking is possible"<<endl;
}
else{
cout<<"hotel booking not possible"<<endl;
}
return 0;
}