Skip to content

Commit f90ae36

Browse files
authored
Create number-of-recent-calls.py
1 parent 58ce124 commit f90ae36

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: Python/number-of-recent-calls.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(1) on average
2+
# Space: O(w), w means the size of the last milliseconds.
3+
4+
import collections
5+
6+
7+
class RecentCounter(object):
8+
9+
def __init__(self):
10+
self.__q = collections.deque()
11+
12+
def ping(self, t):
13+
"""
14+
:type t: int
15+
:rtype: int
16+
"""
17+
self.__q.append(t)
18+
while self.__q[0] < t-3000:
19+
self.__q.popleft()
20+
return len(self.__q)

0 commit comments

Comments
 (0)