Hit Counter
Implement a hit counter which keeps track of number of the number of hits in the last 60 seconds.
add(int timestamp)which addstimestampin seconds in the hit countercount(int timestamp)which returns the number of hits that have been made in the last60seconds, given the current time istimestamp.
You can assume that the timestamps passed into add and count are monotonically increasing.
Constraints
n ≤ 100,000wherenis the number of calls that are made toaddandcount
https://binarysearch.com/problems/Hit-Counter
Examples
Example 1
Input
- methods =
['constructor', 'add', 'add', 'count', 'add', 'count'] - arguments =
[[], [10], [40], [40], [70], [100]]
Output
- answer =
[None, None, None, 2, None, 2]
Explanation
- We create a
HitCounter - We add timestamp
10to the data structure - We add timestamp
40to the data structure - We count the number of timestamps that are within last
60seconds of40. There’s10and40so we return2 - We add timestamp
70to the data structure - We count the number of timestamps that are within last
60seconds of100. There’s40and70so we return2
Leave a comment