DEV Community

Shailesh Kumar
Shailesh Kumar

Posted on

No. Of Recent Calls | Leetcode October Day 1

Problem -
"""
You have a RecentCounter class which counts the number of recent requests within a certain time frame.

Implement the RecentCounter class:

RecentCounter() Initializes the counter with zero recent requests.
int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t].
It is guaranteed that every call to ping uses a strictly larger value of t than the previous call.

Example 1:

Input
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3
"""

Solution -
Intution- Using queue, remove the stale requests which are older than 3000 seconds

from collections import deque
class RecentCounter:

    def __init__(self):
        self.q = deque()

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

Top comments (0)