Problem statement

https://binarysearch.com/problems/Rate-Limiter/

Solution

Equal to Leetcode 0359. Logger Rate Limiter.

Complexity

Time is O(1), space is O(n).

Code

class RateLimiter:
    def __init__(self, x):
        self.d = {}
        self.x = x
        
    def limit(self, message, timestamp):
        if message not in self.d:
            self.d[message] = timestamp
            return False
        else:
            if self.d[message] + self.x > timestamp:
                return True
            else:
                self.d[message] = timestamp
                return False