subject : Original link ( Simple )
solution
Time complexity
Spatial complexity
Execution time
Ans 1 (Python)
–
–
344ms (74.43%)
Ans 2 (Python)
–
–
376ms (42.68%)
Ans 3 (Python)
LeetCode Of Python Execution time , As long as there is no significant difference in time complexity , The execution time is generally of the same order of magnitude , For reference only .
Solution 1 ( Use arrays to implement ):
class RecentCounter:
def __init__(self):
self.queue = []
def ping(self, t: int) -> int:
self.queue.append(t)
while self.queue[0] < t - 3000:
self.queue.pop(0)
return len(self.queue)
Solution 2 ( Use collections.deque Realization )
class RecentCounter:
def __init__(self):
self.queue = collections.deque()
def ping(self, t: int) -> int:
self.queue.append(t)
while self.queue[0] < t - 3000:
self.queue.popleft()
return len(self.queue)