Skip to content

Commit 7cc6c2b

Browse files
committed
Add solution and test-cases for problem 1797
1 parent a01e89c commit 7cc6c2b

File tree

4 files changed

+99
-28
lines changed

4 files changed

+99
-28
lines changed
Loading

leetcode/1701-1800/1797.Design-Authentication-Manager/README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [1797.Design Authentication Manager][title]
22

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-
63
## 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`.
75

8-
**Example 1:**
6+
Implement the `AuthenticationManager` class:
97

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.
1412

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.
1714

18-
## 题解
15+
**Example 1:**
1916

20-
### 思路1
21-
> ...
22-
Design Authentication Manager
23-
```go
24-
```
17+
![1](./1.png)
2518

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+
```
2636

2737
## 结语
2838

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type AuthenticationManager struct {
4+
live int64
5+
token map[string]int64
6+
}
7+
8+
func Constructor(timeToLive int) AuthenticationManager {
9+
return AuthenticationManager{live: int64(timeToLive), token: make(map[string]int64)}
10+
}
11+
12+
func (this *AuthenticationManager) Generate(tokenId string, currentTime int) {
13+
this.token[tokenId] = int64(currentTime) + this.live
14+
}
15+
16+
func (this *AuthenticationManager) Renew(tokenId string, currentTime int) {
17+
expired, ok := this.token[tokenId]
18+
if !ok {
19+
return
20+
}
21+
ic := int64(currentTime)
22+
if expired > ic {
23+
this.token[tokenId] = ic + this.live
24+
}
25+
}
26+
27+
func (this *AuthenticationManager) CountUnexpiredTokens(currentTime int) int {
28+
ans := 0
29+
ic := int64(currentTime)
30+
for _, c := range this.token {
31+
if c > ic {
32+
ans++
33+
}
34+
}
35+
return ans
36+
}
37+
38+
type input struct {
39+
op byte
40+
token string
41+
v int
42+
}
43+
44+
func Solution(n int, inputs []input) []int {
45+
c := Constructor(n)
46+
ans := make([]int, 0)
47+
for _, op := range inputs {
48+
if op.op == 'g' {
49+
c.Generate(op.token, op.v)
50+
continue
51+
}
52+
if op.op == 'r' {
53+
c.Renew(op.token, op.v)
54+
continue
55+
}
56+
ans = append(ans, c.CountUnexpiredTokens(op.v))
57+
}
58+
return ans
559
}

leetcode/1701-1800/1797.Design-Authentication-Manager/Solution_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,37 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
n int
14+
inputs []input
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 5, []input{
18+
{'r', "aaa", 1},
19+
{'g', "aaa", 2},
20+
{'c', "", 6},
21+
{'g', "bbb", 7},
22+
{'r', "aaa", 8},
23+
{'r', "bbb", 10},
24+
{'c', "", 15},
25+
}, []int{1, 0}},
1926
}
2027

2128
// 开始测试
2229
for i, c := range cases {
2330
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
31+
got := Solution(c.n, c.inputs)
2532
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
34+
c.expect, got, c.n, c.inputs)
2835
}
2936
})
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}

0 commit comments

Comments
 (0)