Practice/Google/Leetcode 359. Logger Rate Limiter
CodingMust
Design a message throttling system that controls the rate at which messages can be printed or logged. Your system should accept messages along with their timestamps and decide whether each message should be allowed through based on a rate limiting rule.
The rule is simple: once a particular message has been printed, that same message cannot be printed again for the next 10 seconds. Different messages are tracked independently.
Implement the Logger class with the following methods:
__init__(): Initializes the logger objectshouldPrintMessage(timestamp: int, message: str) -> bool: Returns true if the message should be printed at the given timestamp, or false if it should be suppressed. The timestamp is a monotonically increasing integer representing seconds.Example 1:
` Logger logger = new Logger();
logger.shouldPrintMessage(1, "hello"); // Returns true, message "hello" is printed for the first time at timestamp 1
logger.shouldPrintMessage(2, "world"); // Returns true, message "world" is printed for the first time at timestamp 2
logger.shouldPrintMessage(3, "hello"); // Returns false, "hello" was already printed at timestamp 1, and 3 - 1 < 10
logger.shouldPrintMessage(11, "hello"); // Returns true, enough time (11 - 1 = 10 seconds) has passed since "hello" was last printed `
Example 2:
` Logger logger = new Logger();
logger.shouldPrintMessage(0, "status"); // Returns true
logger.shouldPrintMessage(5, "status"); // Returns false, only 5 seconds have passed
logger.shouldPrintMessage(10, "status"); // Returns true, exactly 10 seconds have passed
logger.shouldPrintMessage(15, "error"); // Returns true, different message `