class SmallestInfiniteSet {
public:
SmallestInfiniteSet() {
}
int popSmallest() {
}
void addBack(int num) {
}
};
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
* int param_1 = obj->popSmallest();
* obj->addBack(num);
*/
type SmallestInfiniteSet struct {
thres int
s *treeset.Set
}
func Constructor() SmallestInfiniteSet {
return SmallestInfiniteSet{
thres:1,
s:treeset.NewWithIntComparator(),
}
}
func (this *SmallestInfiniteSet) PopSmallest() int {
if this.s.Empty() {
ans := this.thres
this.thres++
return ans
}
it := this.s.Iterator()
it.Next()
ans := it.Value().(int)
this.s.Remove(ans)
return ans
}
func (this *SmallestInfiniteSet) AddBack(num int) {
if num < this.thres {
this.s.Add(num)
}
}
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.PopSmallest();
* obj.AddBack(num);
*/
from sortedcontainers import SortedSet
class SmallestInfiniteSet:
def __init__(self):
self.thres = 1
self.s = SortedSet()
def popSmallest(self) -> int:
s_ = self.s
if not s_:
ans = self.thres
self.thres += 1
return ans
ans = s_[0]
s_.pop(0)
return ans
def addBack(self, num: int) -> None:
s_ = self.s
if num < self.thres:
s_.add(num)
# Your SmallestInfiniteSet object will be instantiated and called as such:
# obj = SmallestInfiniteSet()
# param_1 = obj.popSmallest()
# obj.addBack(num)
class SmallestInfiniteSet {
private int thres;
private TreeSet<Integer> set;
public SmallestInfiniteSet() {
thres = 1;
set = new TreeSet<Integer>();
}
public int popSmallest() {
if (set.isEmpty()) {
int ans = thres;
++thres;
return ans;
}
int ans = set.pollFirst();
return ans;
}
public void addBack(int num) {
if (num < thres) {
set.add(num);
}
}
}
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet obj = new SmallestInfiniteSet();
* int param_1 = obj.popSmallest();
* obj.addBack(num);
*/
class SmallestInfiniteSet {
public:
SmallestInfiniteSet() {}
int popSmallest() {
if (s.empty()) {
int ans = thres;
++thres;
return ans;
}
int ans = *s.begin();
s.erase(s.begin());
return ans;
}
void addBack(int num) {
if (num < thres) {
s.insert(num);
}
}
private:
int thres = 1;
set<int> s;
};
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
* int param_1 = obj->popSmallest();
* obj->addBack(num);
*/
class SmallestInfiniteSet {
public:
set<int>s;
SmallestInfiniteSet() {
s.clear();
for(int i=1;i<=1010;i++)s.insert(i);
}
int popSmallest() {
int x=*s.begin();
s.erase(s.begin());
return x;
}
void addBack(int num) {
s.insert(num);
}
};
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* SmallestInfiniteSet* obj = new SmallestInfiniteSet();
* int param_1 = obj->popSmallest();
* obj->addBack(num);
*/
class SmallestInfiniteSet:
def __init__(self):
self.p = list(range(1,1001))
heapq.heapify(self.p)
self.cur = set()
def popSmallest(self) -> int:
t = heapq.heappop(self.p)
self.cur.add(t)
return t
def addBack(self, num: int) -> None:
if num in self.cur:
heapq.heappush(self.p, num)
self.cur.remove(num)
# Your SmallestInfiniteSet object will be instantiated and called as such:
# obj = SmallestInfiniteSet()
# param_1 = obj.popSmallest()
# obj.addBack(num)
type SmallestInfiniteSet map[int]bool
func Constructor() SmallestInfiniteSet { return SmallestInfiniteSet{} }
func (s SmallestInfiniteSet) PopSmallest() int {
for i := 1; ; i++ {
if !s[i] {
s[i] = true
return i
}
}
}
func (s SmallestInfiniteSet) AddBack(n int) {
delete(s, n)
}
/**
* Your SmallestInfiniteSet object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.PopSmallest();
* obj.AddBack(num);
*/