문제가 특이하다.
일단 모든 양의 정수를 담아둘 수 있는 곳은 없기 때문에, 당연히 내부적으로는 특정 변수의 증감으로 표현해야 함 (next)
생성자
next 변수를 1로 초기화.
나중에 쓸 우선순위 큐도 미리 초기화.
addBack
popSmallest
class SmallestInfiniteSet {
private int next;
private PriorityQueue<Integer> pq;
public SmallestInfiniteSet() {
next = 1;
pq = new PriorityQueue<>();
}
public int popSmallest() {
if (!pq.isEmpty()) {
return pq.poll();
}
return next++;
}
public void addBack(int num) {
if (num < next && !pq.contains(num)) {
pq.offer(num);
}
}
}