# time&space O(min(N,K)) K: 3000
class RecentCounter:
def __init__(self):
self.q = collections.deque()
def ping(self, t: int) -> int:
while self.q and t-self.q[0] > 3000:
self.q.popleft()
self.q.append(t)
return len(self.q)
# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)
TODO: bucket solution
Binary Search Solution
class RecentCounter:
# time O(lgN) space O(min(N,3000))
def __init__(self):
self.li = []
def ping(self, t: int) -> int:
self.li.append(t)
idx = bisect.bisect_left(self.li,t-3000)
return len(self.li)-idx