class HitCounter(object): def __init__(self): """ Initialize your data structure here. """ self.hits = collections.OrderedDict() self.lastTimestamp = 0 def hit(self, timestamp): """ Record a hit. @param timestamp - The current timestamp (in seconds granularity). :type timestamp: int :rtype: void """ if timestamp not in self.hits: if len(self.hits) == 300: self.hits.popitem(last=False) self.hits[timestamp] = 0 self.hits[timestamp] += 1 def getHits(self, timestamp): """ Return the number of hits in the past 5 minutes. @param timestamp - The current timestamp (in seconds granularity). :type timestamp: int :rtype: int """ return sum([y for x,y in self.hits.items() if x + 300 > timestamp] ) # Your HitCounter object will be instantiated and called as such: # obj = HitCounter() # obj.hit(timestamp) # param_2 = obj.getHits(timestamp)
38ms precise python code
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.