
Leetcode Problem 933: Number of Recent Calls
Implement a RecentCounter class that tracks the number of recent network requests within a 3,000-millisecond sliding window. It must support:
RecentCounter() — Initializes the counter with no requests.ping(t) — Records a new request at time t milliseconds and returns the count of all requests in the inclusive range [t − 3000, t].All calls to ping are made with strictly increasing timestamps.
Constraints:
ping will be made.class RecentCounter {
constructor(
private requests: number[] = []
) { }
ping(t: number): number {
let lowerBoundRequests = t - 3000
this.requests.push(t)
while (this.requests[0] < lowerBoundRequests) {
this.requests.shift()
}
return this.requests.length
}
}