Number of recent calls
Standard · Write code · about 10 min
A server receives calls at the times in times (milliseconds, strictly increasing). For each call at time t, report how many calls happened in the window [t - 3000, t], including this one.
Return the list of counts, one per call.
Example 1
Input:
[[1,100,3001,3002]]
Output:
[1,2,3,3]
At t = 3002 the window starts at 2, so the call at time 1 has dropped out.
Example 2
Input:
[[1000,2000,3000,4000,7001]]
Output:
[1,2,3,4,1]
Constraints
- 1 ≤ times.length ≤ 10, 000
- 1 ≤ times[i] ≤ 10^9
- times is strictly increasing
Learn the idea first: Queues: enqueue, dequeue; circular buffer, a visual lesson.
More Queues: enqueue, dequeue; circular buffer problems
- Which value leaves first? Warm-up
- Predict head and tail Warm-up
- Implement a circular queue Standard
- Spot the bug: the tail runs off the end Standard
- Moving average from a stream Challenge