Solve the Number of Recent Calls Challenge in Java
The challenge
You have a RecentCounter
class which counts the number of recent requests within a certain time frame.
Implement the RecentCounter
class:
RecentCounter()
Initializes the counter with zero recent requests.int ping(int t)
Adds a new request at timet
, wheret
represents some time in milliseconds, and returns the number of requests that has happened in the past3000
milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range[t - 3000, t]
.
It is guaranteed that every call to ping
uses a strictly larger value of t
than the previous call.
Example 1:
|
|
Constraints:
1 <= t <= 10<sup>9</sup>
- Each test case will call
ping
with strictly increasing values oft
. - At most
10<sup>4</sup>
calls will be made toping
.
The solution in Java
This problem requires using a Queue
which we create from a LinkedList
.
|
|