Design Authentication Manager LeetCode solution in Python

1
# Runtime: 732 ms (Top 5.07%) | Memory: 15.5 MB (Top 65.88%)
2
class AuthenticationManager(object):
3

4
def __init__(self, timeToLive):
5
self.token = dict()
6
self.time = timeToLive # store timeToLive and create dictionary
7

8
def generate(self, tokenId, currentTime):
9
self.token[tokenId] = currentTime # store tokenId with currentTime
10

11
def renew(self, tokenId, currentTime):
12
limit = (
13
currentTime - self.time
14
) # calculate limit time to filter unexpired tokens
15
if (
16
tokenId in self.token and self.token[tokenId] > limit
17
): # filter tokens and renew its time
18
self.token[tokenId] = currentTime
19

20
def countUnexpiredTokens(self, currentTime):
21
limit = (
22
currentTime - self.time
23
) # calculate limit time to filter unexpired tokens
24
c = 0
25
for i in self.token:
26
if self.token[i] > limit: # count unexpired tokens
27
c += 1
28
return c

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0