Skip to content

Commit 9e913a4

Browse files
committed
feat: add solutions to leetcode problem: No.1797. Design Authentication Manager
1 parent 76738ff commit 9e913a4

File tree

5 files changed

+207
-8
lines changed

5 files changed

+207
-8
lines changed

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## Introduction
1616

17-
Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF](https://leetcode-cn.com/problemset/lcof/) and [LCCI](https://leetcode-cn.com/problemset/lcci/) problems, updated daily.
17+
Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF](https://leetcode-cn.com/problemset/lcof/) and [LCCI](https://leetcode-cn.com/problemset/lcci/) problems, updated daily. Please give me a [star](https://github.com/doocs/leetcode) 🌟 if you like it.
1818

1919
[中文文档](/README.md)
2020

solution/1700-1799/1797.Design Authentication Manager/README.md

+71-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ authenticationManager.<code>renew</code>("aaa", 1); // 时刻 1 时,没有验
3636
authenticationManager.generate("aaa", 2); // 时刻 2 时,生成一个 tokenId 为 "aaa" 的新验证码。
3737
authenticationManager.<code>countUnexpiredTokens</code>(6); // 时刻 6 时,只有 tokenId 为 "aaa" 的验证码未过期,所以返回 1 。
3838
authenticationManager.generate("bbb", 7); // 时刻 7 时,生成一个 tokenId 为 "bbb" 的新验证码。
39-
authenticationManager.<code>renew</code>("aaa", 8); // tokenId 为 "aaa" 的验证码在时刻 7 过期,且 8 >= 7 ,所以时刻 8 的renew 操作被忽略,没有验证码被更新。
39+
authenticationManager.<code>renew</code>("aaa", 8); // tokenId 为 "aaa" 的验证码在时刻 7 过期,且 8 >= 7 ,所以时刻 8 的 renew 操作被忽略,没有验证码被更新。
4040
authenticationManager.<code>renew</code>("bbb", 10); // tokenId 为 "bbb" 的验证码在时刻 10 没有过期,所以 renew 操作会执行,该 token 将在时刻 15 过期。
4141
authenticationManager.<code>countUnexpiredTokens</code>(15); // tokenId 为 "bbb" 的验证码在时刻 15 过期,tokenId 为 "aaa" 的验证码在时刻 7 过期,所有验证码均已过期,所以返回 0 。
42+
4243
</pre>
4344

4445
<p> </p>
@@ -55,27 +56,93 @@ authenticationManager.<code>countUnexpiredTokens</code>(15); // tokenId 为 "bbb
5556
<li>所有函数的调用次数总共不超过 <code>2000</code> 次。</li>
5657
</ul>
5758

58-
5959
## 解法
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
用哈希表存放 token 与对应的过期时间。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
<!-- 这里可写当前语言的特殊实现逻辑 -->
6870

6971
```python
70-
72+
class AuthenticationManager:
73+
74+
def __init__(self, timeToLive: int):
75+
self.timeToLive = timeToLive
76+
self.tokens = {}
77+
78+
def generate(self, tokenId: str, currentTime: int) -> None:
79+
self.tokens[tokenId] = currentTime + self.timeToLive
80+
81+
def renew(self, tokenId: str, currentTime: int) -> None:
82+
expire_time = self.tokens.get(tokenId)
83+
if expire_time is None or expire_time <= currentTime:
84+
return
85+
self.tokens[tokenId] = currentTime + self.timeToLive
86+
87+
def countUnexpiredTokens(self, currentTime: int) -> int:
88+
unexpiredCount = 0
89+
for val in self.tokens.values():
90+
if val > currentTime:
91+
unexpiredCount += 1
92+
return unexpiredCount
93+
94+
95+
# Your AuthenticationManager object will be instantiated and called as such:
96+
# obj = AuthenticationManager(timeToLive)
97+
# obj.generate(tokenId,currentTime)
98+
# obj.renew(tokenId,currentTime)
99+
# param_3 = obj.countUnexpiredTokens(currentTime)
71100
```
72101

73102
### **Java**
74103

75104
<!-- 这里可写当前语言的特殊实现逻辑 -->
76105

77106
```java
78-
107+
class AuthenticationManager {
108+
private int timeToLive;
109+
private Map<String, Integer> tokens;
110+
111+
public AuthenticationManager(int timeToLive) {
112+
this.timeToLive = timeToLive;
113+
tokens = new HashMap<>();
114+
}
115+
116+
public void generate(String tokenId, int currentTime) {
117+
tokens.put(tokenId, currentTime + timeToLive);
118+
}
119+
120+
public void renew(String tokenId, int currentTime) {
121+
Integer expireTime = tokens.get(tokenId);
122+
if (expireTime == null || expireTime <= currentTime) {
123+
return;
124+
}
125+
tokens.put(tokenId, currentTime + timeToLive);
126+
}
127+
128+
public int countUnexpiredTokens(int currentTime) {
129+
int unexpiredCount = 0;
130+
for (Integer val : tokens.values()) {
131+
if (val > currentTime) {
132+
++unexpiredCount;
133+
}
134+
}
135+
return unexpiredCount;
136+
}
137+
}
138+
139+
/**
140+
* Your AuthenticationManager object will be instantiated and called as such:
141+
* AuthenticationManager obj = new AuthenticationManager(timeToLive);
142+
* obj.generate(tokenId,currentTime);
143+
* obj.renew(tokenId,currentTime);
144+
* int param_3 = obj.countUnexpiredTokens(currentTime);
145+
*/
79146
```
80147

81148
### **...**

solution/1700-1799/1797.Design Authentication Manager/README_EN.md

+68-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ authenticationManager.generate(&quot;bbb&quot;, 7); // Generates a new token wit
3636
authenticationManager.<code>renew</code>(&quot;aaa&quot;, 8); // The token with tokenId &quot;aaa&quot; expired at time 7, and 8 &gt;= 7, so at time 8 the <code>renew</code> request is ignored, and nothing happens.
3737
authenticationManager.<code>renew</code>(&quot;bbb&quot;, 10); // The token with tokenId &quot;bbb&quot; is unexpired at time 10, so the <code>renew</code> request is fulfilled and now the token will expire at time 15.
3838
authenticationManager.<code>countUnexpiredTokens</code>(15); // The token with tokenId &quot;bbb&quot; expires at time 15, and the token with tokenId &quot;aaa&quot; expired at time 7, so currently no token is unexpired, so return 0.
39+
3940
</pre>
4041

4142
<p>&nbsp;</p>
@@ -51,21 +52,85 @@ authenticationManager.<code>countUnexpiredTokens</code>(15); // The token with t
5152
<li>At most <code>2000</code> calls will be made to all functions combined.</li>
5253
</ul>
5354

54-
5555
## Solutions
5656

5757
<!-- tabs:start -->
5858

5959
### **Python3**
6060

6161
```python
62-
62+
class AuthenticationManager:
63+
64+
def __init__(self, timeToLive: int):
65+
self.timeToLive = timeToLive
66+
self.tokens = {}
67+
68+
def generate(self, tokenId: str, currentTime: int) -> None:
69+
self.tokens[tokenId] = currentTime + self.timeToLive
70+
71+
def renew(self, tokenId: str, currentTime: int) -> None:
72+
expire_time = self.tokens.get(tokenId)
73+
if expire_time is None or expire_time <= currentTime:
74+
return
75+
self.tokens[tokenId] = currentTime + self.timeToLive
76+
77+
def countUnexpiredTokens(self, currentTime: int) -> int:
78+
unexpiredCount = 0
79+
for val in self.tokens.values():
80+
if val > currentTime:
81+
unexpiredCount += 1
82+
return unexpiredCount
83+
84+
85+
# Your AuthenticationManager object will be instantiated and called as such:
86+
# obj = AuthenticationManager(timeToLive)
87+
# obj.generate(tokenId,currentTime)
88+
# obj.renew(tokenId,currentTime)
89+
# param_3 = obj.countUnexpiredTokens(currentTime)
6390
```
6491

6592
### **Java**
6693

6794
```java
68-
95+
class AuthenticationManager {
96+
private int timeToLive;
97+
private Map<String, Integer> tokens;
98+
99+
public AuthenticationManager(int timeToLive) {
100+
this.timeToLive = timeToLive;
101+
tokens = new HashMap<>();
102+
}
103+
104+
public void generate(String tokenId, int currentTime) {
105+
tokens.put(tokenId, currentTime + timeToLive);
106+
}
107+
108+
public void renew(String tokenId, int currentTime) {
109+
Integer expireTime = tokens.get(tokenId);
110+
if (expireTime == null || expireTime <= currentTime) {
111+
return;
112+
}
113+
tokens.put(tokenId, currentTime + timeToLive);
114+
}
115+
116+
public int countUnexpiredTokens(int currentTime) {
117+
int unexpiredCount = 0;
118+
for (Integer val : tokens.values()) {
119+
if (val > currentTime) {
120+
++unexpiredCount;
121+
}
122+
}
123+
return unexpiredCount;
124+
}
125+
}
126+
127+
/**
128+
* Your AuthenticationManager object will be instantiated and called as such:
129+
* AuthenticationManager obj = new AuthenticationManager(timeToLive);
130+
* obj.generate(tokenId,currentTime);
131+
* obj.renew(tokenId,currentTime);
132+
* int param_3 = obj.countUnexpiredTokens(currentTime);
133+
*/
69134
```
70135

71136
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class AuthenticationManager {
2+
private int timeToLive;
3+
private Map<String, Integer> tokens;
4+
5+
public AuthenticationManager(int timeToLive) {
6+
this.timeToLive = timeToLive;
7+
tokens = new HashMap<>();
8+
}
9+
10+
public void generate(String tokenId, int currentTime) {
11+
tokens.put(tokenId, currentTime + timeToLive);
12+
}
13+
14+
public void renew(String tokenId, int currentTime) {
15+
Integer expireTime = tokens.get(tokenId);
16+
if (expireTime == null || expireTime <= currentTime) {
17+
return;
18+
}
19+
tokens.put(tokenId, currentTime + timeToLive);
20+
}
21+
22+
public int countUnexpiredTokens(int currentTime) {
23+
int unexpiredCount = 0;
24+
for (Integer val : tokens.values()) {
25+
if (val > currentTime) {
26+
++unexpiredCount;
27+
}
28+
}
29+
return unexpiredCount;
30+
}
31+
}
32+
33+
/**
34+
* Your AuthenticationManager object will be instantiated and called as such:
35+
* AuthenticationManager obj = new AuthenticationManager(timeToLive);
36+
* obj.generate(tokenId,currentTime);
37+
* obj.renew(tokenId,currentTime);
38+
* int param_3 = obj.countUnexpiredTokens(currentTime);
39+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class AuthenticationManager:
2+
3+
def __init__(self, timeToLive: int):
4+
self.timeToLive = timeToLive
5+
self.tokens = {}
6+
7+
def generate(self, tokenId: str, currentTime: int) -> None:
8+
self.tokens[tokenId] = currentTime + self.timeToLive
9+
10+
def renew(self, tokenId: str, currentTime: int) -> None:
11+
expire_time = self.tokens.get(tokenId)
12+
if expire_time is None or expire_time <= currentTime:
13+
return
14+
self.tokens[tokenId] = currentTime + self.timeToLive
15+
16+
def countUnexpiredTokens(self, currentTime: int) -> int:
17+
unexpiredCount = 0
18+
for val in self.tokens.values():
19+
if val > currentTime:
20+
unexpiredCount += 1
21+
return unexpiredCount
22+
23+
24+
# Your AuthenticationManager object will be instantiated and called as such:
25+
# obj = AuthenticationManager(timeToLive)
26+
# obj.generate(tokenId,currentTime)
27+
# obj.renew(tokenId,currentTime)
28+
# param_3 = obj.countUnexpiredTokens(currentTime)

0 commit comments

Comments
 (0)