0506|933. 最近的请求次数
- 队列的动态存储,不需要的值就及时抛弃(每次把超时的踢掉)
this
的使用,原型链时间长了有点忘。
复杂度:
- 时间复杂度:均摊 O(1),每个元素最多 入队 / 出队 各一次。
- 空间复杂度:O(L),L 为队列的元素个数。
var RecentCounter = function () {
this.queue = [];
};
RecentCounter.prototype.ping = function (t) {
this.queue.push(t);
while ( this.queue[0] < t - 3000 ) {
this.queue.shift();
}
return this.queue.length;
};