Fetch the repository succeeded.
# Time: O(1), amortized
# Space: O(k), k is the count of seconds.
from collections import deque
class HitCounter(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.__k = 300
self.__dq = deque()
self.__count = 0
def hit(self, timestamp):
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
:type timestamp: int
:rtype: void
"""
self.getHits(timestamp)
if self.__dq and self.__dq[-1][0] == timestamp:
self.__dq[-1][1] += 1
else:
self.__dq.append([timestamp, 1])
self.__count += 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
"""
while self.__dq and self.__dq[0][0] <= timestamp - self.__k:
self.__count -= self.__dq.popleft()[1]
return self.__count
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。