Skip to content

Commit

Permalink
feat(C++): Add 1845
Browse files Browse the repository at this point in the history
To revisit. Small optimizations can be done to avoid pre-initialization
  • Loading branch information
euchangxian committed Oct 11, 2024
1 parent 0c5cb1e commit 05c9625
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions C++/1845-SeatReservationManager/Solution.cpp
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);
*/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ Now solving in C++. Like it.
| 1791 | FindCenterOfStarGraph | [![C++](assets/c++.svg)](C++/1791-FindCenterOfStarGraph/Solution.cpp) |
| 1813 | SentenceSimilarityThree | [![C++](assets/c++.svg)](C++/1813-SentenceSimilarityThree/Solution.cpp) |
| 1823 | FindTheWinnerOfTheCircularGame | [![C++](assets/c++.svg)](C++/1823-FindTheWinnerOfTheCircularGame/Solution.cpp) |
| 1845 | SeatReservationManager | [![C++](assets/c++.svg)](C++/1845-SeatReservationManager/Solution.cpp) |
| 1863 | SumOfAllSubsetXORTotals | [![C++](assets/c++.svg)](C++/1863-SumOfAllSubsetXORTotals/Solution.cpp) |
| 1884 | EggDropWith2EggsAndNFloors | [![Go](assets/go.svg)](Go/1884-EggDropWith2EggsAndNFloors/Solution.go) |
| 1894 | FindTheStudentThatWillReplaceTheChalk | [![C++](assets/c++.svg)](C++/1894-FindTheStudentThatWillReplaceTheChalk/Solution.cpp) |
Expand Down

0 comments on commit 05c9625

Please sign in to comment.