from heapq import heappop, heappushclassPhoneDirectory:def__init__(self,maxNumbers:int): self.used =set()# 记录已被使用的编号 self.heap = [] # 按照号码的值由小到大进行分配 self.next =0 self.k = maxNumbers # 电话目录的最大编号defget(self) ->int: ans =-1iflen(self.used)== self.k:return ansif self.heap: ans =heappop(self.heap)else: ans, self.next = self.next, self.next+1 self.used.add(ans)return ans defcheck(self,n:int) ->bool:return n < self.k and n notin self.useddefrelease(self,n:int):if n in self.used: self.used.remove(n)heappush(self.heap, n)