← 返回 apple 的题目列表Implement Hit Counter
类型:online_judge
Implement a Hit Counter that records request timestamps and returns the number of hits in the most recent 300 seconds (5 minutes).
Implement:
hit(timestamp: int) -> None
getHits(timestamp: int) -> int
hit(timestamp) records one request at timestamp.
getHits(timestamp) returns the number of hits in the inclusive window [timestamp - 299, timestamp].
Timestamps are supplied in non-decreasing order.
Multiple hits may occur in the same second.
Avoid storing one separate record per hit when possible; implement a space-efficient solution.
CLI Input Format
The first line contains the number of operations q. Each following line is one operation:
hit timestamp
get timestamp
Print one result for every get operation.
Example
Input:
5
hit 1
hit 2
hit 300
get 300
get 301
Output:
3
2
Example
Input
5
hit 1
hit 2
hit 300
get 300
get 301
Output
3
2