Skip to content

933. Number of Recent Calls 👎

  • Time:
    • Constructor: $O(1)$
    • ping(): $O(3000) = O(1)$
  • Space: $O(3000) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class RecentCounter {
 public:
  int ping(int t) {
    q.push(t);
    while (q.front() < t - 3000)
      q.pop();
    return q.size();
  }

 private:
  queue<int> q;
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class RecentCounter {
  public int ping(int t) {
    q.offer(t);
    while (q.peek() < t - 3000)
      q.poll();
    return q.size();
  }

  private Queue<Integer> q = new LinkedList<>();
}
1
2
3
4
5
6
7
8
9
class RecentCounter:
  def __init__(self):
    self.q = collections.deque()

  def ping(self, t: int) -> int:
    self.q.append(t)
    while self.q[0] < t - 3000:
      self.q.popleft()
    return len(self.q)