ZhongKuo0228 / study

0 stars 0 forks source link

933. Number of Recent Calls #85

Open fockspaces opened 12 months ago

fockspaces commented 12 months ago

用 queue 來做,判斷 queue 中的 element 何時消失

class RecentCounter:

    def __init__(self):
        self.queue = []

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

# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)