-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0359-logger-rate-limiter.py
More file actions
30 lines (24 loc) · 869 Bytes
/
0359-logger-rate-limiter.py
File metadata and controls
30 lines (24 loc) · 869 Bytes
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
# time complexity: O(1)
# space complexity: O(n)
from collections import defaultdict
class Logger:
def __init__(self):
self.logDict = defaultdict(int)
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
if message not in self.logDict:
self.logDict[message] = timestamp + 10
return True
else:
if timestamp < self.logDict[message]:
return False
else:
self.logDict[message] = timestamp + 10
return True
# Your Logger object will be instantiated and called as such:
obj = Logger()
print(obj.shouldPrintMessage(1, "foo"))
print(obj.shouldPrintMessage(2, "bar"))
print(obj.shouldPrintMessage(3, "foo"))
print(obj.shouldPrintMessage(8, "bar"))
print(obj.shouldPrintMessage(10, "foo"))
print(obj.shouldPrintMessage(11, "foo"))