Number of Recent Calls
Problem Summary
(Write in your own words, not copied from LeetCode. This forces comprehension.)
- You're to build an object that lets you ping at a certain time. You have to return how many pings you got in the last 3000ms.
Why This Works
(Explain the core reason the solution is correct.)
- You are guaranteed to get increasing ping time.
- Therefore, you don't need to store anything less than 3000 of every ping received
Main Concepts Used
(Mark the CS concepts or algorithms used.)
Time & Space Complexity
- Time:
→ Reason: - Space:
→ Reason:
Code
class RecentCounter:
rangeInMs = 3000
def __init__(self):
self.queue = deque()
def ping(self, t: int) -> int:
self.queue.append(t)
rangeStart = t - self.rangeInMs
while self.queue and self.queue[0] < rangeStart:
self.queue.popleft()
return len(self.queue)