-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1797.design-authentication-manager.py
53 lines (44 loc) · 1.77 KB
/
1797.design-authentication-manager.py
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
53
#
# @lc app=leetcode id=1797 lang=python3
#
# [1797] Design Authentication Manager
#
# @lc code=start
# TAGS: Hash Table, Design
class AuthenticationManager:
"""
132 ms, 94.83%.
Time: O(Design). Space: O(N)
"""
def __init__(self, timeToLive: int):
self.expire_time = {}
self.queue = collections.deque()
self.lifespan = timeToLive
def generate(self, tokenId: str, currentTime: int) -> None:
self.checkExpired(currentTime)
# Update expire_time and add it queue
self.expire_time[tokenId] = currentTime + self.lifespan
self.queue.append((self.expire_time[tokenId], tokenId))
def renew(self, tokenId: str, currentTime: int) -> None:
self.checkExpired(currentTime)
if tokenId in self.expire_time:
# Update expire_time and add it queue
self.expire_time[tokenId] = currentTime + self.lifespan
self.queue.append((self.expire_time[tokenId], tokenId))
def countUnexpiredTokens(self, currentTime: int) -> int:
self.checkExpired(currentTime)
return len(self.expire_time)
# Clean up queue and expire_time. TIme: O(N)
def checkExpired(self, currentTime):
# while expired time <= currentTime, pop it
while self.queue and self.queue[0][0] <= currentTime:
time, id_ = self.queue.popleft()
# If expired time == latest expire_time, del
if time == self.expire_time[id_] and self.expire_time[id_] <= currentTime:
del self.expire_time[id_]
# Your AuthenticationManager object will be instantiated and called as such:
# obj = AuthenticationManager(timeToLive)
# obj.generate(tokenId,currentTime)
# obj.renew(tokenId,currentTime)
# param_3 = obj.countUnexpiredTokens(currentTime)
# @lc code=end