Number of Recent Calls LeetCode solution in Python

1
# Runtime: 676 ms (Top 17.97%) | Memory: 19.4 MB (Top 70.89%)
2
class RecentCounter:
3
# Here we use list to store ping details.
4
def __init__(self):
5
self.store = []
6

7
def ping(self, t: int) -> int:
8
# Basically what we need to return is how many pings fall in the range(t-3000, t).
9
# So here we append every t. Now in loop how many t from left side < t-3000, we just pop them
10
# and return the length of the list, which'd contain elements in range(t-3000, t).
11
# And since every t is going to greater than previous, we don't need to think about duplicates.
12

13
self.store.append(t)
14

15
while self.store[0] < t - 3000:
16
self.store.pop(0)
17

18
return len(self.store)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0