ZhongKuo0228 / study

0 stars 0 forks source link

2336. Smallest Number in Infinite Set #107

Open fockspaces opened 9 months ago

fockspaces commented 9 months ago

since there's only limit 1000 call counts, we can assume the potential operating numbers are below 1000.

  1. create heapq that contains 1000 integer in order
  2. init the heap
  3. popSmallest => heappop
  4. addBack => if not in list, add the num into heapq
class SmallestInfiniteSet:
    heap = []
    def __init__(self):
        self.heap = [i for i in range(1, 1001)]
        heapify(self.heap)

    def popSmallest(self) -> int:
        return heappop(self.heap)

    def addBack(self, num: int) -> None:
        if num not in self.heap:
            heappush(self.heap, num)

# Your SmallestInfiniteSet object will be instantiated and called as such:
# obj = SmallestInfiniteSet()
# param_1 = obj.popSmallest()
# obj.addBack(num)