Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac03426

Browse files
authored
feat: [LeetCode #933] Number Of Recent Calls (#11)
Тип: Queue Сложность: easy Временная сложность: O(1) Пространственная сложность: O(n) - Ссылка: https://leetcode.com/problems/number-of-recent-calls/
1 parent 7a9aa15 commit ac03426

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

‎src/queue/__init__.py

Whitespace-only changes.

‎src/queue/number_of_recent_calls/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import deque
2+
3+
4+
class RecentCounter:
5+
def __init__(self):
6+
self.deque = deque([])
7+
self._INTERVAL = 3000
8+
9+
def ping(self, t: int) -> int:
10+
previous = t - self._INTERVAL
11+
12+
while self.deque and self.deque[0] < previous:
13+
self.deque.popleft()
14+
15+
self.deque.append(t)
16+
17+
return len(self.deque)

‎tests/test_number_of_recent_calls.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from src.queue.number_of_recent_calls.solution import RecentCounter
3+
4+
5+
@pytest.mark.parametrize(
6+
"calls, args, expected",
7+
[
8+
(
9+
["RecentCounter", "ping", "ping", "ping", "ping"],
10+
[[], [1], [100], [3001], [3002]],
11+
[None, 1, 2, 3, 3],
12+
),
13+
],
14+
)
15+
def test_recent_counter(calls, args, expected):
16+
obj = None
17+
results = []
18+
19+
for call, arg, exp in zip(calls, args, expected):
20+
if call == "RecentCounter":
21+
obj = RecentCounter()
22+
results.append(None)
23+
else:
24+
method = getattr(obj, call)
25+
res = method(*arg)
26+
results.append(res)
27+
28+
assert results == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /