Description
Design a logger system that receives a stream of messages along with their timestamps. Each unique message should only be printed at most every 10 seconds (i.e. a message printed at timestamp t will prevent other identical messages from being printed until timestamp t + 10).
All messages will come in chronological order. Several messages may arrive at the same timestamp.
Implement the Logger class:
- Logger() Initializes the logger object.
- bool shouldPrintMessage(int timestamp, string message) Returns true if the message should be printed in the given timestamp, otherwise returns false.
Example 1:
Input
["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
Output
[null, true, true, false, false, false, true]
Explanation
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo"); // return true, next allowed timestamp for "foo" is 1 + 10 = 11
logger.shouldPrintMessage(2, "bar"); // return true, next allowed timestamp for "bar" is 2 + 10 = 12
logger.shouldPrintMessage(3, "foo"); // 3 < 11, return false
logger.shouldPrintMessage(8, "bar"); // 8 < 12, return false
logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false
logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is 11 + 10 = 21
Constraints:
0 <= timestamp <= 10^9
Every timestamp will be passed in non-decreasing order (chronological order).
1 <= message.length <= 30
At most 104 calls will be made to shouldPrintMessage.
Solution
Hashmap
One easy way to do so is use a hash map to store the message’s last appeared time, and compare it with the current timestamp to decide if we want to print it or not.
Time complexity:
o
(
n
)
o(n)
o(n) where n
is the time we call this function
Space complexity:
o
(
n
)
o(n)
o(n)
Set+queue
Use a queue to store all the messages, and pop those stale ones.
Code
Hashmap
class Logger:
def __init__(self):
self.message_last_time = {}
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
should_print = (self.message_last_time.get(message, -11) <= timestamp - 10)
if should_print:
self.message_last_time[message] = timestamp
return should_print
# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)
Set+queue
class Logger:
def __init__(self):
self.messages = set()
# [(message, timestamp)]
self.messages_chron = collections.deque([])
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
while self.messages_chron and self.messages_chron[0][1] <= timestamp - 10:
old_message, _ = self.messages_chron.popleft()
self.messages.remove(old_message)
should_print = not (message in self.messages)
if should_print:
self.messages_chron.append((message, timestamp))
self.messages.add(message)
return should_print
# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)