Skip to content

Commit f46ffd0

Browse files
committed
feat: add solutions to lc problem: No.1797
No.1797.Design Authentication Manager
1 parent a52ac55 commit f46ffd0

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed

solution/1700-1799/1797.Design Authentication Manager/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,96 @@ class AuthenticationManager {
145145
*/
146146
```
147147

148+
### **TypeScript**
149+
150+
```ts
151+
class AuthenticationManager {
152+
private timeToLive: number;
153+
private map: Map<string, number>;
154+
155+
constructor(timeToLive: number) {
156+
this.timeToLive = timeToLive;
157+
this.map = new Map<string, number>();
158+
}
159+
160+
generate(tokenId: string, currentTime: number): void {
161+
this.map.set(tokenId, currentTime + this.timeToLive);
162+
}
163+
164+
renew(tokenId: string, currentTime: number): void {
165+
if ((this.map.get(tokenId) ?? 0) <= currentTime) {
166+
return;
167+
}
168+
this.map.set(tokenId, currentTime + this.timeToLive);
169+
}
170+
171+
countUnexpiredTokens(currentTime: number): number {
172+
let res = 0;
173+
for (const time of this.map.values()) {
174+
if (time > currentTime) {
175+
res++;
176+
}
177+
}
178+
return res;
179+
}
180+
}
181+
182+
/**
183+
* Your AuthenticationManager object will be instantiated and called as such:
184+
* var obj = new AuthenticationManager(timeToLive)
185+
* obj.generate(tokenId,currentTime)
186+
* obj.renew(tokenId,currentTime)
187+
* var param_3 = obj.countUnexpiredTokens(currentTime)
188+
*/
189+
```
190+
191+
### **Rust**
192+
193+
```rust
194+
use std::collections::HashMap;
195+
struct AuthenticationManager {
196+
time_to_live: i32,
197+
map: HashMap<String, i32>,
198+
}
199+
200+
/**
201+
* `&self` means the method takes an immutable reference.
202+
* If you need a mutable reference, change it to `&mut self` instead.
203+
*/
204+
impl AuthenticationManager {
205+
fn new(timeToLive: i32) -> Self {
206+
Self {
207+
time_to_live: timeToLive,
208+
map: HashMap::new(),
209+
}
210+
}
211+
212+
fn generate(&mut self, token_id: String, current_time: i32) {
213+
self.map.insert(token_id, current_time + self.time_to_live);
214+
}
215+
216+
fn renew(&mut self, token_id: String, current_time: i32) {
217+
if self.map.get(&token_id).unwrap_or(&0) <= &current_time {
218+
return;
219+
}
220+
self.map.insert(token_id, current_time + self.time_to_live);
221+
}
222+
223+
fn count_unexpired_tokens(&self, current_time: i32) -> i32 {
224+
self.map.values().filter(|&time| *time > current_time).count() as i32
225+
}
226+
}
227+
228+
229+
/**
230+
* Your AuthenticationManager object will be instantiated and called as such:
231+
* let obj = AuthenticationManager::new(timeToLive);
232+
* obj.generate(tokenId, currentTime);
233+
* obj.renew(tokenId, currentTime);
234+
* let ret_3: i32 = obj.count_unexpired_tokens(currentTime);
235+
*/
236+
```
237+
148238
### **...**
149239

150240
```

solution/1700-1799/1797.Design Authentication Manager/README_EN.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,96 @@ class AuthenticationManager {
133133
*/
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
class AuthenticationManager {
140+
private timeToLive: number;
141+
private map: Map<string, number>;
142+
143+
constructor(timeToLive: number) {
144+
this.timeToLive = timeToLive;
145+
this.map = new Map<string, number>();
146+
}
147+
148+
generate(tokenId: string, currentTime: number): void {
149+
this.map.set(tokenId, currentTime + this.timeToLive);
150+
}
151+
152+
renew(tokenId: string, currentTime: number): void {
153+
if ((this.map.get(tokenId) ?? 0) <= currentTime) {
154+
return;
155+
}
156+
this.map.set(tokenId, currentTime + this.timeToLive);
157+
}
158+
159+
countUnexpiredTokens(currentTime: number): number {
160+
let res = 0;
161+
for (const time of this.map.values()) {
162+
if (time > currentTime) {
163+
res++;
164+
}
165+
}
166+
return res;
167+
}
168+
}
169+
170+
/**
171+
* Your AuthenticationManager object will be instantiated and called as such:
172+
* var obj = new AuthenticationManager(timeToLive)
173+
* obj.generate(tokenId,currentTime)
174+
* obj.renew(tokenId,currentTime)
175+
* var param_3 = obj.countUnexpiredTokens(currentTime)
176+
*/
177+
```
178+
179+
### **Rust**
180+
181+
```rust
182+
use std::collections::HashMap;
183+
struct AuthenticationManager {
184+
time_to_live: i32,
185+
map: HashMap<String, i32>,
186+
}
187+
188+
/**
189+
* `&self` means the method takes an immutable reference.
190+
* If you need a mutable reference, change it to `&mut self` instead.
191+
*/
192+
impl AuthenticationManager {
193+
fn new(timeToLive: i32) -> Self {
194+
Self {
195+
time_to_live: timeToLive,
196+
map: HashMap::new(),
197+
}
198+
}
199+
200+
fn generate(&mut self, token_id: String, current_time: i32) {
201+
self.map.insert(token_id, current_time + self.time_to_live);
202+
}
203+
204+
fn renew(&mut self, token_id: String, current_time: i32) {
205+
if self.map.get(&token_id).unwrap_or(&0) <= &current_time {
206+
return;
207+
}
208+
self.map.insert(token_id, current_time + self.time_to_live);
209+
}
210+
211+
fn count_unexpired_tokens(&self, current_time: i32) -> i32 {
212+
self.map.values().filter(|&time| *time > current_time).count() as i32
213+
}
214+
}
215+
216+
217+
/**
218+
* Your AuthenticationManager object will be instantiated and called as such:
219+
* let obj = AuthenticationManager::new(timeToLive);
220+
* obj.generate(tokenId, currentTime);
221+
* obj.renew(tokenId, currentTime);
222+
* let ret_3: i32 = obj.count_unexpired_tokens(currentTime);
223+
*/
224+
```
225+
136226
### **...**
137227

138228
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::collections::HashMap;
2+
struct AuthenticationManager {
3+
time_to_live: i32,
4+
map: HashMap<String, i32>,
5+
}
6+
7+
/**
8+
* `&self` means the method takes an immutable reference.
9+
* If you need a mutable reference, change it to `&mut self` instead.
10+
*/
11+
impl AuthenticationManager {
12+
fn new(timeToLive: i32) -> Self {
13+
Self {
14+
time_to_live: timeToLive,
15+
map: HashMap::new(),
16+
}
17+
}
18+
19+
fn generate(&mut self, token_id: String, current_time: i32) {
20+
self.map.insert(token_id, current_time + self.time_to_live);
21+
}
22+
23+
fn renew(&mut self, token_id: String, current_time: i32) {
24+
if self.map.get(&token_id).unwrap_or(&0) <= &current_time {
25+
return;
26+
}
27+
self.map.insert(token_id, current_time + self.time_to_live);
28+
}
29+
30+
fn count_unexpired_tokens(&self, current_time: i32) -> i32 {
31+
self.map.values().filter(|&time| *time > current_time).count() as i32
32+
}
33+
}
34+
35+
36+
/**
37+
* Your AuthenticationManager object will be instantiated and called as such:
38+
* let obj = AuthenticationManager::new(timeToLive);
39+
* obj.generate(tokenId, currentTime);
40+
* obj.renew(tokenId, currentTime);
41+
* let ret_3: i32 = obj.count_unexpired_tokens(currentTime);
42+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class AuthenticationManager {
2+
private timeToLive: number;
3+
private map: Map<string, number>;
4+
5+
constructor(timeToLive: number) {
6+
this.timeToLive = timeToLive;
7+
this.map = new Map<string, number>();
8+
}
9+
10+
generate(tokenId: string, currentTime: number): void {
11+
this.map.set(tokenId, currentTime + this.timeToLive);
12+
}
13+
14+
renew(tokenId: string, currentTime: number): void {
15+
if ((this.map.get(tokenId) ?? 0) <= currentTime) {
16+
return;
17+
}
18+
this.map.set(tokenId, currentTime + this.timeToLive);
19+
}
20+
21+
countUnexpiredTokens(currentTime: number): number {
22+
let res = 0;
23+
for (const time of this.map.values()) {
24+
if (time > currentTime) {
25+
res++;
26+
}
27+
}
28+
return res;
29+
}
30+
}
31+
32+
/**
33+
* Your AuthenticationManager object will be instantiated and called as such:
34+
* var obj = new AuthenticationManager(timeToLive)
35+
* obj.generate(tokenId,currentTime)
36+
* obj.renew(tokenId,currentTime)
37+
* var param_3 = obj.countUnexpiredTokens(currentTime)
38+
*/

0 commit comments

Comments
 (0)