코딩테스트

[leetcode] 2336. Smallest Number in Infinite Set

nayoon 2024. 6. 18. 22:57

문제

 

PriorityQueue를 이용해서 만들었더니 80ms 가량 걸림

 

class SmallestInfiniteSet {
    private PriorityQueue<Integer> pQ;
    public SmallestInfiniteSet() {
        pQ = new PriorityQueue<>();
        for(int i = 1; i < 1001; i++) {
            pQ.add(i);
        }
    }
    
    public int popSmallest() {
        return pQ.poll();
    }
    
    public void addBack(int num) {
        if(pQ.contains(num)) {
            return;
        }

        pQ.add(num);
    }
}

/**
 * Your SmallestInfiniteSet object will be instantiated and called as such:
 * SmallestInfiniteSet obj = new SmallestInfiniteSet();
 * int param_1 = obj.popSmallest();
 * obj.addBack(num);
 */