Skip to content

Commit 7bfdf3a

Browse files
committed
D. J.:
- Added the documentation of the leetcode problem 146 - Added the leetcode problem and solution for 933
1 parent a26f805 commit 7bfdf3a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
- [909 Snakes and Ladders](https://leetcode.com/problems/snakes-and-ladders/description/)
256256
- [918 Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/description/)
257257
- [931 Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/description/)
258+
- [933 Number of Recent Cells](https://leetcode.com/problems/number-of-recent-calls/description/)
258259
- [983 Minimum Cost for Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/description/)
259260
- [1027 Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/description/)
260261
- [1035 Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/description/)

awesome_python_leetcode/_146_lru_cache.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ def __init__(self, key, val, prev=None, next=None):
77

88

99
class LRUCache:
10+
"""
11+
Design a data structure that follows the constraints of a Least Recently Used (LRU)
12+
cache.
13+
14+
Implement the LRUCache class:
15+
- LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
16+
- int get(int key) Return the value of the key if the key exists, otherwise return
17+
-1.
18+
- void put(int key, int value) Update the value of the key if the key exists.
19+
Otherwise, add the key-value pair to the cache. If the number of keys exceeds the
20+
capacity from this operation, evict the least recently used key.
21+
22+
The functions get and put must each run in O(1) average time complexity.
23+
"""
1024

1125
def __init__(self, capacity: int):
1226
self.cap = capacity
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class RecentCounter:
2+
"""
3+
You have a RecentCounter class which counts the number of recent requests within
4+
a certain time frame.
5+
6+
Implement the RecentCounter class:
7+
- RecentCounter() Initializes the counter with zero recent requests.
8+
- int ping(int t) Adds a new request at time t, where t represents some time in
9+
milliseconds, and returns the number of requests that has happened in the past
10+
3000 milliseconds (including the new request). Specifically, return the number
11+
of requests that have happened in the inclusive range [t - 3000, t].
12+
13+
It is guaranteed that every call to ping uses a strictly larger value of t than
14+
the previous call.
15+
"""
16+
17+
def __init__(self):
18+
self.pings = []
19+
20+
def ping(self, t: int) -> int:
21+
# Remove all items that are smaller than t - 3000
22+
while len(self.pings):
23+
if self.pings[0] >= t - 3000:
24+
break
25+
self.pings.pop(0)
26+
27+
# Append new ping and return the length of the queue
28+
self.pings.append(t)
29+
return len(self.pings)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from awesome_python_leetcode._933_number_of_recent_calls import RecentCounter
2+
3+
4+
def test_func():
5+
"""Tests the solution of a LeetCode problem."""
6+
counter = RecentCounter()
7+
assert 1 == counter.ping(1)
8+
assert 2 == counter.ping(100)
9+
assert 3 == counter.ping(3001)
10+
assert 3 == counter.ping(3002)

0 commit comments

Comments
 (0)