diff --git a/leetcode/1701-1800/1797.Design-Authentication-Manager/1.png b/leetcode/1701-1800/1797.Design-Authentication-Manager/1.png new file mode 100644 index 000000000..864ba1394 Binary files /dev/null and b/leetcode/1701-1800/1797.Design-Authentication-Manager/1.png differ diff --git a/leetcode/1701-1800/1797.Design-Authentication-Manager/README.md b/leetcode/1701-1800/1797.Design-Authentication-Manager/README.md index 6da0de40a..69c5294fe 100755 --- a/leetcode/1701-1800/1797.Design-Authentication-Manager/README.md +++ b/leetcode/1701-1800/1797.Design-Authentication-Manager/README.md @@ -1,28 +1,38 @@ # [1797.Design Authentication Manager][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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`. -**Example 1:** +Implement the `AuthenticationManager` class: -``` -Input: a = "11", b = "1" -Output: "100" -``` +- `AuthenticationManager(int timeToLive)` constructs the `AuthenticationManager` and sets the `timeToLive`. +- `generate(string tokenId, int currentTime)` generates a new token with the given `tokenId` at the given `currentTime` in seconds. +- `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. +- `countUnexpiredTokens(int currentTime)` returns the number of **unexpired** tokens at the given currentTime. -## 题意 -> ... +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. -## 题解 +**Example 1:** -### 思路1 -> ... -Design Authentication Manager -```go -``` +![1](./1.png) +``` +Input +["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"] +[[5], ["aaa", 1], ["aaa", 2], [6], ["bbb", 7], ["aaa", 8], ["bbb", 10], [15]] +Output +[null, null, null, 1, null, null, null, 0] + +Explanation +AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with timeToLive = 5 seconds. +authenticationManager.renew("aaa", 1); // No token exists with tokenId "aaa" at time 1, so nothing happens. +authenticationManager.generate("aaa", 2); // Generates a new token with tokenId "aaa" at time 2. +authenticationManager.countUnexpiredTokens(6); // The token with tokenId "aaa" is the only unexpired one at time 6, so return 1. +authenticationManager.generate("bbb", 7); // Generates a new token with tokenId "bbb" at time 7. +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. +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. +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. +``` ## 结语 diff --git a/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution.go b/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution.go index d115ccf5e..c6e4a42e9 100644 --- a/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution.go +++ b/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution.go @@ -1,5 +1,59 @@ package Solution -func Solution(x bool) bool { - return x +type AuthenticationManager struct { + live int64 + token map[string]int64 +} + +func Constructor(timeToLive int) AuthenticationManager { + return AuthenticationManager{live: int64(timeToLive), token: make(map[string]int64)} +} + +func (this *AuthenticationManager) Generate(tokenId string, currentTime int) { + this.token[tokenId] = int64(currentTime) + this.live +} + +func (this *AuthenticationManager) Renew(tokenId string, currentTime int) { + expired, ok := this.token[tokenId] + if !ok { + return + } + ic := int64(currentTime) + if expired > ic { + this.token[tokenId] = ic + this.live + } +} + +func (this *AuthenticationManager) CountUnexpiredTokens(currentTime int) int { + ans := 0 + ic := int64(currentTime) + for _, c := range this.token { + if c > ic { + ans++ + } + } + return ans +} + +type input struct { + op byte + token string + v int +} + +func Solution(n int, inputs []input) []int { + c := Constructor(n) + ans := make([]int, 0) + for _, op := range inputs { + if op.op == 'g' { + c.Generate(op.token, op.v) + continue + } + if op.op == 'r' { + c.Renew(op.token, op.v) + continue + } + ans = append(ans, c.CountUnexpiredTokens(op.v)) + } + return ans } diff --git a/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution_test.go b/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution_test.go index 14ff50eb4..a4e003ec5 100644 --- a/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution_test.go +++ b/leetcode/1701-1800/1797.Design-Authentication-Manager/Solution_test.go @@ -10,30 +10,37 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + n int + inputs []input + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", 5, []input{ + {'r', "aaa", 1}, + {'g', "aaa", 2}, + {'c', "", 6}, + {'g', "bbb", 7}, + {'r', "aaa", 8}, + {'r', "bbb", 10}, + {'c', "", 15}, + }, []int{1, 0}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.n, c.inputs) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.n, c.inputs) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }