-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathimplement-router.py
56 lines (48 loc) · 1.52 KB
/
implement-router.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Time: ctor: O(1)
# addPacket: O(logn)
# forwardPacket: O(logn)
# getCount: O(logn)
# Space: O(n)
import collections
from sortedcontainers import SortedList
# queue, sorted list
class Router(object):
def __init__(self, memoryLimit):
"""
:type memoryLimit: int
"""
self.__size = memoryLimit
self.__q = collections.deque()
self.__lookup = collections.defaultdict(SortedList)
def addPacket(self, source, destination, timestamp):
"""
:type source: int
:type destination: int
:type timestamp: int
:rtype: bool
"""
if (timestamp, source) in self.__lookup[destination]:
return False
self.__lookup[destination].add((timestamp, source))
if len(self.__q) == self.__size:
s, d, t = self.__q.popleft()
self.__lookup[d].remove((t, s))
self.__q.append((source, destination, timestamp))
return True
def forwardPacket(self):
"""
:rtype: List[int]
"""
if not self.__q:
return []
s, d, t = self.__q.popleft()
self.__lookup[d].remove((t, s))
return [s, d, t]
def getCount(self, destination, startTime, endTime):
"""
:type destination: int
:type startTime: int
:type endTime: int
:rtype: int
"""
return self.__lookup[destination].bisect_left((endTime+1, 0))-self.__lookup[destination].bisect_left((startTime, 0))