Leetcode #359 (Python): Logger Rate Limiter

정욱·2021년 4월 26일
0

Leetcode

목록 보기
30/38

Logger Rate Limiter

  • Difficulty: Easy
  • Type: Hash
  • link

Problem


Solution

  • Time Complexity: O(n) (n = number of queries, look up time is constant)
  • Space Complexity: O(m) (m = number of messages)
import collections
class Logger:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.logs = {}

    def shouldPrintMessage(self, timestamp: int, message: str) -> bool:
        """
        Returns true if the message should be printed in the given timestamp, otherwise returns false.
        If this method returns false, the message will not be printed.
        The timestamp is in seconds granularity.
        """
        if message in self.logs and  self.logs[message] + 10 <= timestamp:
            self.logs[message] = timestamp
            return True
        elif message not in self.logs:
            self.logs[message] = timestamp
            return True
        else:
            return False


# Your Logger object will be instantiated and called as such:
# obj = Logger()
# param_1 = obj.shouldPrintMessage(timestamp,message)

0개의 댓글