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);
*/
'코딩테스트' 카테고리의 다른 글
[leetcode] 1004. Max Consecutive One III (0) | 2024.06.25 |
---|---|
[leetcode] 1456.Maximum Number of Vowels in a Substring of Given Length (0) | 2024.06.24 |
[leetcode] 216. Combination Sum III (0) | 2024.06.15 |
[leetcode] 994. Rotting Oranges (0) | 2024.06.14 |
[leetcode] 1161. Maximum Level Sum of a Binary Tree (0) | 2024.06.14 |