1. 题目来源
2. 题目解析
思路:
- 小顶堆模拟即可。
- 时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn)
- 空间复杂度: O ( n ) O(n) O(n)
代码:
class SeatManager {
public:
priority_queue<int, vector<int>, greater<int>> heap;
SeatManager(int n) {
for (int i = 1; i <= n; i ++ ) heap.push(i);
}
int reserve() {
int t = heap.top();
heap.pop();
return t;
}
void unreserve(int seatNumber) {
heap.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);
*/