forked from Ritesh25696/interviewBit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotel Bookings Possible
More file actions
54 lines (43 loc) · 1.56 KB
/
Copy pathHotel Bookings Possible
File metadata and controls
54 lines (43 loc) · 1.56 KB
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
47
48
49
50
51
52
53
54
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. Write a program that solves this problem in time O(N log N) .
Input:
First list for arrival time of booking.
Second list for departure time of booking.
Third is K which denotes count of rooms.
Output:
A boolean which tells whether its possible to make a booking.
Return 0/1 for C programs.
O -> No there are not enough rooms for N booking.
1 -> Yes there are enough rooms for N booking.
Example :
Input :
Arrivals : [1 3 5]
Departures : [2 6 8]
K : 1
Return : False / 0
At day = 5, there are 2 guests in the hotel. But I have only one room.
See Expected Output
NotesAll Notes
bool Solution::hotel(vector<int> &arrive, vector<int> &depart, int K) {
sort(arrive.begin(), arrive.end());
sort(depart.begin(), depart.end());
int len, A, D, start, end;
len = arrive.size();
start = arrive[0];
end = depart[len-1];
A = D = 0;
for (;start<=end;start++) {
while((D<len)&&(start==depart[D])) {
K++;
D++;
}
while((A<len)&&(start==arrive[A])) {
if(K>0)
K--;
else
return false;
A++;
}
}
return true;
}