Logger Rate Limiter — LeetCode 359 Python Solution
EasyLeetCode PremiumDesignHash TableData Stream
- Problem
- #359
- Pattern
- Hash Map
- Reading time
- 3 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- ["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
- Output
- [null, true, true, false, false, false, true]
- Explanation
- Logger logger = new Logger();
Python solution
Python
class Logger:
def __init__(self):
self.ts = {}
def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
t = self.ts.get(message, 0)
if t > timestamp:
return False
self.ts[message] = timestamp + 10
return True
# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)Complexity
| Measure | Complexity |
|---|---|
| Time | O(1) |
| Space | O(m), where m is the number of distinct messages auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 359. Logger Rate Limiter is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 359. Logger Rate Limiter?
- LeetCode 359. Logger Rate Limiter is rated Easy on LeetCode.
- What is the time complexity of LeetCode 359. Logger Rate Limiter?
- The Python solution on this page runs in O(1).
- What is the space complexity of LeetCode 359. Logger Rate Limiter?
- The Python solution on this page uses O(m), where m is the number of distinct messages auxiliary space.
- What topics does LeetCode 359. Logger Rate Limiter cover?
- LeetCode 359. Logger Rate Limiter is tagged Design, Hash Table and Data Stream on LeetCode.
- Is LeetCode 359. Logger Rate Limiter a premium problem?
- Yes. LeetCode 359. Logger Rate Limiter is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.