-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To revisit. Small optimizations can be done to avoid pre-initialization
- Loading branch information
1 parent
0c5cb1e
commit 05c9625
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <cstddef> | ||
#include <functional> | ||
#include <queue> | ||
#include <vector> | ||
|
||
using namespace std; | ||
class SeatManager { | ||
private: | ||
priority_queue<int, vector<int>, greater<>> availableSeats; | ||
|
||
public: | ||
SeatManager(int n) { | ||
for (int i = 1; i <= n; ++i) { | ||
availableSeats.push(i); | ||
} | ||
} | ||
|
||
// Fetch smallest unreserved seat, reserves it and returns the number | ||
int reserve() { | ||
int smallestSeat = availableSeats.top(); | ||
availableSeats.pop(); | ||
return smallestSeat; | ||
} | ||
|
||
// unreserves the seat with the given seatNumber | ||
void unreserve(int seatNumber) { availableSeats.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); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters