933. Number of Recent Calls
Problem Description
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
(in milliseconds), wheret
represents some time in the past, and returns the number of requests that have happened in the last3000
milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range[t - 3000, t]
.
Example 1:
Input
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]
Explanation
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
Constraints:
1 <= t <= 10^9
- Each test case will call
ping
with strictly increasing values oft
. - At most
10^4
calls will be made toping
.
Solution
Python Solution
class RecentCounter:
def __init__(self):
self.requests = []
def ping(self, t: int) -> int:
self.requests.append(t)
while self.requests[0] < t - 3000:
self.requests.pop(0)
return len(self.requests)
Time Complexity: O(1) amortized
Each element is added and removed at most once.
Space Complexity: O(n)
We need to store all requests within the 3000ms window.
Java Solution
class RecentCounter {
private Queue requests;
public RecentCounter() {
requests = new LinkedList<>();
}
public int ping(int t) {
requests.offer(t);
while (requests.peek() < t - 3000) {
requests.poll();
}
return requests.size();
}
}
Time Complexity: O(1) amortized
Each element is added and removed at most once.
Space Complexity: O(n)
We need to store all requests within the 3000ms window.
C++ Solution
class RecentCounter {
private:
queue requests;
public:
RecentCounter() {}
int ping(int t) {
requests.push(t);
while (requests.front() < t - 3000) {
requests.pop();
}
return requests.size();
}
};
Time Complexity: O(1) amortized
Each element is added and removed at most once.
Space Complexity: O(n)
We need to store all requests within the 3000ms window.
JavaScript Solution
var RecentCounter = function() {
this.requests = [];
};
/**
* @param {number} t
* @return {number}
*/
RecentCounter.prototype.ping = function(t) {
this.requests.push(t);
while (this.requests[0] < t - 3000) {
this.requests.shift();
}
return this.requests.length;
};
Time Complexity: O(1) amortized
Each element is added and removed at most once.
Space Complexity: O(n)
We need to store all requests within the 3000ms window.
C# Solution
public class RecentCounter {
private Queue requests;
public RecentCounter() {
requests = new Queue();
}
public int Ping(int t) {
requests.Enqueue(t);
while (requests.Peek() < t - 3000) {
requests.Dequeue();
}
return requests.Count;
}
}
Time Complexity: O(1) amortized
Each element is added and removed at most once.
Space Complexity: O(n)
We need to store all requests within the 3000ms window.