#933
Number of Recent Calls
newbie · 265 · lc easy +19 · 78.2% accepted · 835 likes · top 89%
Description
Design a RecentCounter that measures how many requests fall within a rolling 3-second window:
- RecentCounter() starts with no recorded requests.
- int ping(int t) logs a request at time t milliseconds and returns the count of requests in the window [t - 3000, t]. Each call provides a strictly larger t than the previous.
Example 1:
Input
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]
Example 2:
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
Code
1
2
3
4
5
6
7
8
9
10
11
12