362. Design Hit Counter
保持记录的头尾timestamp
差值不超过300即可。
class HitCounter:
def __init__(self):
"""
Initialize your data structure here.
"""
self.clicks = []
def hit(self, timestamp: int) -> None:
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
"""
self.clicks.append(timestamp)
while self.clicks and self.clicks[-1] - self.clicks[0] >= 300:
self.clicks.pop(0)
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 self.clicks and timestamp - self.clicks[0] >= 300:
self.clicks.pop(0)
return len(self.clicks)
# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)
Last updated
Was this helpful?