LeetCode 359: Logger Rate Limiter Solution
Master LeetCode problem 359 (Logger Rate Limiter), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
359. Logger Rate Limiter
Problem Explanation
Explanation:
The problem asks us to design a logger system that allows messages to be printed if and only if the timestamp is within 10 seconds of the previous timestamp for the same message. If a message with the same content is sent within 10 seconds of the previous message with the same content, it should be ignored. Otherwise, it should be printed.
To solve this problem, we can use a HashMap to store the message content as keys and their corresponding timestamps as values. Whenever a new message comes in, we check if the message has been seen before within the last 10 seconds. If not, we update the timestamp and print the message.
:
Solution Code
class Logger {
private Map<String, Integer> messageMap;
public Logger() {
messageMap = new HashMap<>();
}
public boolean shouldPrintMessage(int timestamp, String message) {
if (!messageMap.containsKey(message) || timestamp - messageMap.get(message) >= 10) {
messageMap.put(message, timestamp);
return true;
}
return false;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 359 (Logger Rate Limiter)?
This page provides optimized solutions for LeetCode problem 359 (Logger Rate Limiter) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 359 (Logger Rate Limiter)?
The time complexity for LeetCode 359 (Logger Rate Limiter) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 359 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 359 in Java, C++, or Python.