-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0933_Number_of_Recent_Calls.py
More file actions
53 lines (33 loc) · 964 Bytes
/
Copy path0933_Number_of_Recent_Calls.py
File metadata and controls
53 lines (33 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
class RecentCounter:
def __init__(self):
self.times = deque()
def ping(self, t: int) -> int:
self.times.append(t)
while self.times and self.times[0] < t - 3000:
self.times.popleft()
return len(self.times)
solution
"""
from typing import *
from collections import *
# general idea
# use deque as the container, because once the old request
# is passed, they can be safely removed
# algorithm ?
# time complexity
# O(n - valid request)
# space complexity
class RecentCounter:
def __init__(self):
self.counter = deque() # use deque as counter
def ping(self, t: int) -> int:
# edge cases / impossible cases
# normal cases
self.counter.append(t) # add the new request
while self.counter and self.counter[0] < t-3000:
self.counter.popleft()
return len(self.counter)
pass
if __name__ == "__main__":
print()