1797. Design Authentication Manager Medium
1/**
2 * [1797] Design Authentication Manager
3 *
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.
5 * Implement the AuthenticationManager class:
6 *
7 * AuthenticationManager(int timeToLive) constructs the AuthenticationManager and sets the timeToLive.
8 * generate(string tokenId, int currentTime) generates a new token with the given tokenId at the given currentTime in seconds.
9 * 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.
10 * countUnexpiredTokens(int currentTime) returns the number of unexpired tokens at the given currentTime.
11 *
12 * 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.
13 *
14 * Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/copy-of-pc68_q2.png" style="width: 500px; height: 287px;" />
16 * Input
17 * ["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"]
18 * [[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]]
19 * Output
20 * [null, null, null, 1, null, null, null, 0]
21 * Explanation
22 * AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with timeToLive = 5 seconds.
23 * authenticationManager.renew("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens.
24 * authenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2.
25 * authenticationManager.countUnexpiredTokens(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1.
26 * authenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7.
27 * 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.
28 * 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.
29 * 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.
30 *
31 *
32 * Constraints:
33 *
34 * 1 <= timeToLive <= 10^8
35 * 1 <= currentTime <= 10^8
36 * 1 <= tokenId.length <= 5
37 * tokenId consists only of lowercase letters.
38 * All calls to generate will contain unique values of tokenId.
39 * The values of currentTime across all the function calls will be strictly increasing.
40 * At most 2000 calls will be made to all functions combined.
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/design-authentication-manager/
46// discuss: https://leetcode.com/problems/design-authentication-manager/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50struct AuthenticationManager {
51 false
52 }
53
54
55/**
56 * `&self` means the method takes an immutable reference.
57 * If you need a mutable reference, change it to `&mut self` instead.
58 */
59impl AuthenticationManager {
60
61 fn new(timeToLive: i32) -> Self {
62
63 }
64
65 fn generate(&self, token_id: String, current_time: i32) {
66
67 }
68
69 fn renew(&self, token_id: String, current_time: i32) {
70
71 }
72
73 fn count_unexpired_tokens(&self, current_time: i32) -> i32 {
74
75 }
76}
77
78/**
79 * Your AuthenticationManager object will be instantiated and called as such:
80 * let obj = AuthenticationManager::new(timeToLive);
81 * obj.generate(tokenId, currentTime);
82 * obj.renew(tokenId, currentTime);
83 * let ret_3: i32 = obj.count_unexpired_tokens(currentTime);
84 */
85
86// submission codes end
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_1797() {
94 }
95}
96
Back
© 2025 bowen.ge All Rights Reserved.