|
1 | 1 | # [1797.Design Authentication Manager][title]
|
2 | 2 |
|
3 |
| -> [!WARNING|style:flat] |
4 |
| -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 |
| -
|
6 | 3 | ## Description
|
| 4 | +There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire `timeToLive` seconds after the `currentTime`. If the token is renewed, the expiry time will be **extended** to expire `timeToLive` seconds after the (potentially different) `currentTime`. |
7 | 5 |
|
8 |
| -**Example 1:** |
| 6 | +Implement the `AuthenticationManager` class: |
9 | 7 |
|
10 |
| -``` |
11 |
| -Input: a = "11", b = "1" |
12 |
| -Output: "100" |
13 |
| -``` |
| 8 | +- `AuthenticationManager(int timeToLive)` constructs the `AuthenticationManager` and sets the `timeToLive`. |
| 9 | +- `generate(string tokenId, int currentTime)` generates a new token with the given `tokenId` at the given `currentTime` in seconds. |
| 10 | +- `renew(string tokenId, int currentTime)` renews the **unexpired** token with the given `tokenId` at the given `currentTime` in seconds. If there are no unexpired tokens with the given `tokenId`, the request is ignored, and nothing happens. |
| 11 | +- `countUnexpiredTokens(int currentTime)` returns the number of **unexpired** tokens at the given currentTime. |
14 | 12 |
|
15 |
| -## 题意 |
16 |
| -> ... |
| 13 | +Note that if a token expires at time `t`, and another action happens on time t (`renew` or `countUnexpiredTokens`), the expiration takes place **before** the other actions. |
17 | 14 |
|
18 |
| -## 题解 |
| 15 | +**Example 1:** |
19 | 16 |
|
20 |
| -### 思路1 |
21 |
| -> ... |
22 |
| -Design Authentication Manager |
23 |
| -```go |
24 |
| -``` |
| 17 | + |
25 | 18 |
|
| 19 | +``` |
| 20 | +Input |
| 21 | +["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"] |
| 22 | +[[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]] |
| 23 | +Output |
| 24 | +[null, null, null, 1, null, null, null, 0] |
| 25 | +
|
| 26 | +Explanation |
| 27 | +AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with timeToLive = 5 seconds. |
| 28 | +authenticationManager.renew("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens. |
| 29 | +authenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2. |
| 30 | +authenticationManager.countUnexpiredTokens(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1. |
| 31 | +authenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7. |
| 32 | +authenticationManager.renew("aaa", 8); // The token with tokenId "aaa" expired at time 7, and 8 >= 7, so at time 8 the renew request is ignored, and nothing happens. |
| 33 | +authenticationManager.renew("bbb", 10); // The token with tokenId "bbb" is unexpired at time 10, so the renew request is fulfilled and now the token will expire at time 15. |
| 34 | +authenticationManager.countUnexpiredTokens(15); // The token with tokenId "bbb" expires at time 15, and the token with tokenId "aaa" expired at time 7, so currently no token is unexpired, so return 0. |
| 35 | +``` |
26 | 36 |
|
27 | 37 | ## 结语
|
28 | 38 |
|
|
0 commit comments