-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSeat Reservation Manager.cpp
51 lines (36 loc) · 1.17 KB
/
Seat Reservation Manager.cpp
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
/*
Solution by Rahul Surana
***********************************************************
Design a system that manages the reservation state of n seats that are numbered from 1 to n.
Implement the SeatManager class:
SeatManager(int n) Initializes a SeatManager object that will manage n seats numbered from 1 to n.
All seats are initially available.
int reserve() Fetches the smallest-numbered unreserved seat, reserves it, and returns its number.
void unreserve(int seatNumber) Unreserves the seat with the given seatNumber.
***********************************************************
*/
class SeatManager {
public:
int s;
priority_queue<int> x;
SeatManager(int n) {
s = 1;
}
int reserve() {
if(!x.empty()) {
int a = x.top();
x.pop();
return -a;
}
return s++;
}
void unreserve(int seatNumber) {
x.push(-seatNumber);
}
};
/**
* Your SeatManager object will be instantiated and called as such:
* SeatManager* obj = new SeatManager(n);
* int param_1 = obj->reserve();
* obj->unreserve(seatNumber);
*/