package q2336 import "container/heap" type MinHeap []int func (m *MinHeap) Len() int { return len(*m) } func (m *MinHeap) Less(i int, j int) bool { return (*m)[i] < (*m)[j] } func (m *MinHeap) Push(x any) { *m = append(*m, x.(int)) } func (m *MinHeap) Swap(i int, j int) { (*m)[i], (*m)[j] = (*m)[j], (*m)[i] } func (m *MinHeap) Pop() any { ret := (*m)[len(*m)-1] *m = (*m)[:len(*m)-1] return ret } type SmallestInfiniteSet struct { head int added MinHeap } func Constructor() SmallestInfiniteSet { return SmallestInfiniteSet{ head: 1, added: MinHeap{}, } } func (s *SmallestInfiniteSet) PopSmallest() int { if s.added.Len() > 0 { top := s.added[0] for s.added.Len() > 0 && s.added[0] == top { heap.Pop(&s.added) } return top } else { s.head++ return s.head - 1 } } func (s *SmallestInfiniteSet) AddBack(num int) { if num >= s.head { return } heap.Push(&s.added, num) } /** * Your SmallestInfiniteSet object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.PopSmallest(); * obj.AddBack(num); */