Skip to content

Commit bacd984

Browse files
add 1797
1 parent 5a24e65 commit bacd984

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1797|[Design Authentication Manager](https://leetcode.com/problems/design-authentication-manager/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1797.java) ||Medium|HashTable, Design|
1112
|1796|[Second Largest Digit in a String](https://leetcode.com/problems/second-largest-digit-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1796.java) ||Easy|String|
1213
|1792|[Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1792.java) ||Medium|Heap|
1314
|1791|[Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1791.java) ||Medium|Graph|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1797 {
7+
public static class Solution1 {
8+
public static class AuthenticationManager {
9+
10+
int timeToLive;
11+
int currentTime;
12+
Map<String, Integer> map;//tokenId -> expireTime
13+
14+
public AuthenticationManager(int timeToLive) {
15+
this.timeToLive = timeToLive;
16+
this.currentTime = 0;
17+
this.map = new HashMap<>();
18+
}
19+
20+
public void generate(String tokenId, int currentTime) {
21+
map.put(tokenId, currentTime + timeToLive);
22+
}
23+
24+
public void renew(String tokenId, int currentTime) {
25+
Integer expireTime = map.getOrDefault(tokenId, -1);
26+
if (expireTime == -1 || expireTime <= currentTime) {
27+
return;
28+
}
29+
map.put(tokenId, currentTime + timeToLive);
30+
}
31+
32+
public int countUnexpiredTokens(int currentTime) {
33+
Map<String, Integer> tmp = new HashMap<>();
34+
for (String token : map.keySet()) {
35+
if (map.get(token) > currentTime) {
36+
tmp.put(token, map.get(token));
37+
}
38+
}
39+
map.clear();
40+
map.putAll(tmp);
41+
return map.size();
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)