看板 Marginalman 關於我們 聯絡資訊
寫昨天的 姆咪 硬幹能過就懶得多想了 class Router: def __init__(self, memoryLimit: int): self.q = deque() self.limit = memoryLimit self.st = set() self.mp = defaultdict(deque) def addPacket(self, source: int, destination: int, timestamp: int) -> bool : if (source, destination, timestamp) in self.st: return False else: self.q.appendleft((source, destination, timestamp)) self.st.add((source, destination, timestamp)) self.mp[destination].append(timestamp) if len(self.q) > self.limit: pack = self.q.pop() self.st.remove(pack) self.mp[pack[1]].popleft() return True def forwardPacket(self) -> List[int]: if len(self.q) > 0: pack = self.q.pop() self.mp[pack[1]].popleft() self.st.remove(pack) return [pack[0], pack[1], pack[2]] else: return [] def getCount(self, destination: int, startTime: int, endTime: int) -> int: left_i = bisect_left(self.mp[destination], startTime) right_i = bisect_right(self.mp[destination], endTime) # print(self.mp[destination], left_i, right_i) return right_i-left_i # Your Router object will be instantiated and called as such: # obj = Router(memoryLimit) # param_1 = obj.addPacket(source,destination,timestamp) # param_2 = obj.forwardPacket() # param_3 = obj.getCount(destination,startTime,endTime) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.132.58.28 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1758466775.A.0C3.html
sixB: 你好強喔 09/22 23:32