Rate limiters decide whether each incoming task should be accepted based on how many recent accepted tasks are still inside a fixed time window.
In this question, implement rateLimit(timestamps, maxAllowed, windowLen) for a simplified batch setting. The input is a sorted list of task timestamps, and you should return a boolean array where each entry says whether that task is accepted.
This question is intentionally interview-scoped:
rateLimit([1, 3, 5, 6, 8, 9, 10], 2, 5);// [true, true, false, true, true, false, false]
rateLimit([1, 6, 7], 1, 5);// [true, true, false]
In the second example, the task at time 6 is accepted because the accepted task at time 1 is exactly 5 units earlier, so it is outside the window.
rateLimit([2, 2, 2], 2, 3);// [true, true, false]
rateLimit(timestamps, maxAllowed, windowLen)
timestamps (number[]): Sorted, non-decreasing integer timestamps for each task.maxAllowed (number): Maximum number of accepted tasks allowed inside the current sliding window.windowLen (number): Window length in the same time unit as timestamps.Returns a boolean[] of the same length as timestamps.
Each result is:
true if the task is accepted.false if the task is rejected because the sliding window already contains maxAllowed previously accepted tasks.t should count previously accepted tasks whose timestamps satisfy t - previous < windowLen.windowLen units earlier does not count.console.log() 语句将显示在此处。