Problem statement

https://binarysearch.com/problems/Hit-Counter/

Solution

Equal to Leetcode 0362. Design Hit Counter.

Complexity

Amortized time is O(1) for all operations, space is O(60).

Code

class HitCounter:
    def __init__(self):
        self.queue = deque()

    def add(self, timestamp):
        self.queue.append(timestamp)

    def count(self, timestamp):
        while self.queue and self.queue[0] < timestamp - 60:
            self.queue.popleft()
        return len(self.queue)