-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignHitCounter.py
37 lines (29 loc) · 939 Bytes
/
DesignHitCounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import heapq
class HitCounter:
'''
heap - ac
list will also work, but worse time complexity when poping (0)
'''
def __init__(self):
"""
Initialize your data structure here.
"""
self.h = []
def hit(self, timestamp: int) -> None:
"""
Record a hit.
@param timestamp - The current timestamp (in seconds granularity).
"""
heapq.heappush(self.h,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 self.h and self.h[0] <= timestamp - 300:
heapq.heappop(self.h)
return len(self.h)
# Your HitCounter object will be instantiated and called as such:
# obj = HitCounter()
# obj.hit(timestamp)
# param_2 = obj.getHits(timestamp)