一、题目
二、思路
普通思路,大用例过不了
class SeatManager {
private int num;
private List<Integer> seatList;
public SeatManager(int n) {
num = n;
seatList = new ArrayList<>(num);
for (int i = 0; i < n; i++) {
seatList.add(0);
}
}
public int reserve() {
for (int i = 0; i < num; i++) {
if(seatList.get(i) == 0){
seatList.set(i, 1);
return i + 1;
}
}
return num;
}
public void unreserve(int seatNumber) {
seatList.set(seatNumber - 1, 0);
}
}
/**
* Your SeatManager object will be instantiated and called as such:
* SeatManager obj = new SeatManager(n);
* int param_1 = obj.reserve();
* obj.unreserve(seatNumber);
*/
优先队列
class SeatManager {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
public SeatManager(int n) {
for (int i = 1; i <= n; i++) {
priorityQueue.offer(i);
}
}
public int reserve() {
return priorityQueue.poll();
}
public void unreserve(int seatNumber) {
priorityQueue.offer(seatNumber);
}
}
/**
* Your SeatManager object will be instantiated and called as such:
* SeatManager obj = new SeatManager(n);
* int param_1 = obj.reserve();
* obj.unreserve(seatNumber);
*/