写昨天的
姆咪
硬干能过就懒得多想了
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)