var RecentCounter = function () {
this.queue = []
};
RecentCounter.prototype.ping = function (t) {
this.queue.push(t)
let beforeT = t - 3000
while (this.queue[0] < beforeT) {
this.queue.shift()
}
return this.queue.length
};
时间复杂度:O(n)
空间复杂度:O(n)

本文介绍了一种基于滑动窗口的算法实现,通过维护一个队列来跟踪最近3000毫秒内的请求,实现了对网络延迟的有效监测。该算法采用时间戳作为输入,能够动态调整窗口大小,确保高效准确地计算出当前窗口内的请求数量。
488

被折叠的 条评论
为什么被折叠?



