LeetCode 362. [Python] Design Hit Counter
原题链接
中等
作者:
徐辰潇
,
2021-01-08 05:58:15
,
所有人可见
,
阅读 559
class HitCounter:
#TC: O(1)
#SC: O(1)
def __init__(self):
"""
Initialize your data structure here.
"""
self.Q = collections.deque([])
def hit(self, timestamp: int) -> None:
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
"""
self.Q.append(timestamp)
def getHits(self, timestamp: int) -> int:
"""
Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity).
"""
while(len(self.Q) > 0 and self.Q[0] <= timestamp - 300):
self.Q.popleft()
return len(self.Q)
# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)