Design a counter that reports how many calls happened in the last 3000 milliseconds.
Recent Counter
gfgDesign a class that tracks recent calls in a stream of timestamps.
Implement a class RecentCounter with one method:
RecentCounter() initializes the counter.int ping(int t) records a call at time t and returns the number of recorded calls whose timestamps are in the inclusive range [t - 3000, t].Each ping call is made with strictly increasing timestamps.
For every incoming timestamp, efficiently return how many calls occurred in the last 3000 milliseconds, including the current one.
RecentCounter().ping(t) is made.t is a strictly increasing integer timestamp.ping(t), return the count of timestamps x such that t - 3000 <= x <= t.ping calls.Example 1
Input
["RecentCounter", "ping", "ping", "ping", "ping"] [[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]
Explanation
At time 3001, the valid range is [1, 3001], so the calls at 1, 100, and 3001 are counted. At time 3002, all four calls except none older than 2 are still within the last 3000 ms, so the count remains 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.