- 해시 문제
https://leetcode.com/problems/seat-reservation-manager/description/
- 문제 분석
1. SeatManager class 구현 문제
2. reserve() 함수 호출 시, 남아있는 좌석 중 가장 작은 좌석의 Number반환
3. unreserve(int seatNumber) 함수 호출 시, seatNumber 에 해당하는 좌석 다시 저장
- 문제풀이
1. SeatManager constructor 생성시, 우선순위 큐를 생성한다.
1-1. 우선순위 큐에 1번부터 n 번까지 좌석을 저장한다.
1-2. 우선순위 큐 생성시 오름차순으로 저장할 수 있도록 초기화한다.
2. reserve() 함수 호출시 우선순위 큐에서 poll(); 한다.
3. unreserve() 함수 호출시 우선순위 큐에 offer 로 저장한다.
class SeatManager {
private PriorityQueue<Integer> pQueue;
public SeatManager(int n) {
pQueue = new PriorityQueue<>((o1, o2) -> o1-o2);
for(int i = 0; i < n; i++) {
pQueue.offer(i+1);
}
}
public int reserve() {
return pQueue.poll();
}
public void unreserve(int seatNumber) {
pQueue.offer(seatNumber);
}
}
'TIL' 카테고리의 다른 글
[코테연습] 2405. Optimal Partition of String (0) | 2024.06.28 |
---|---|
[코테연습] 1338. Reduce Array Size to The Half (0) | 2024.06.27 |
[코테연습] 921. Minimum Add to Make Parentheses Valid (0) | 2024.06.25 |
[코테연습] 2390. Removing Stars From a String (0) | 2024.06.24 |
[코테연습] 341. Flatten Nested List Iterator (0) | 2024.06.23 |