← 返回归档
力扣简单

力扣-933. 最近的请求次数

写一个 RecentCounter 类来计算特定时间范围内最近的请求。 请你实现 RecentCounter 类: RecentCounter() 初始化计数器,请求数为 0 。 int ping(int t) 在时间 t 添加一个新请求,其中 t 表示以毫秒为单位的某个时间,并返回过去 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t-3000, t] 内发生的请求数。 保证 每次对 ping 的调用都使用比之前更大的 t 值。

Published
Reading
1 分钟
Version
04

遇到“按时间先后进入,并淘汰最早数据”的问题,可以考虑队列。

class RecentCounter {
  private queue: number[]
  private head: number

  constructor() {
    this.queue = []
    this.head = 0
  }

  ping(t: number): number {
    this.queue.push(t)

    while (this.queue[this.head] < t - 3000) {
      this.head++
    }

    return this.queue.length - this.head
  }
}