File tree 3 files changed +137
-2
lines changed
solution/2600-2699/2622.Cache With Time Limit
3 files changed +137
-2
lines changed Original file line number Diff line number Diff line change 69
69
70
70
<!-- 这里可写通用的实现逻辑 -->
71
71
72
+ ** 方法一:哈希表**
73
+
74
+ 我们用哈希表 $cache$ 记录键值对,其中键为整型键 $key$,值为一个数组,数组的第一个元素为整型值 $value$,第二个元素为元素的过期时间 $expire$。
75
+
76
+ 我们实现一个 ` removeExpire ` 方法,用于删除过期的键值对。在 ` set ` 、` get ` 和 ` count ` 方法中,我们先调用 ` removeExpire ` 方法,然后再进行相应的操作。
77
+
78
+ 时间复杂度为 $O(1)$,空间复杂度为 $O(n)$。其中 $n$ 为哈希表 $cache$ 的大小。
79
+
72
80
<!-- tabs:start -->
73
81
74
82
### ** TypeScript**
75
83
76
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
85
78
86
``` ts
79
-
87
+ class TimeLimitedCache {
88
+ private cache: Map <number , [value : number , expire : number ]> = new Map ();
89
+
90
+ constructor () {}
91
+
92
+ set(key : number , value : number , duration : number ): boolean {
93
+ this .removeExpire ();
94
+ const ans = this .cache .has (key );
95
+ this .cache .set (key , [value , this .now () + duration ]);
96
+ return ans ;
97
+ }
98
+
99
+ get(key : number ): number {
100
+ this .removeExpire ();
101
+ return this .cache .get (key )?.[0 ] ?? - 1 ;
102
+ }
103
+
104
+ count(): number {
105
+ this .removeExpire ();
106
+ return this .cache .size ;
107
+ }
108
+
109
+ private now(): number {
110
+ return new Date ().getTime ();
111
+ }
112
+
113
+ private removeExpire(): void {
114
+ const now = this .now ();
115
+ for (const [key, [, expire]] of this .cache ) {
116
+ if (expire <= now ) {
117
+ this .cache .delete (key );
118
+ }
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Your TimeLimitedCache object will be instantiated and called as such:
125
+ * var obj = new TimeLimitedCache()
126
+ * obj.set(1, 42, 1000); // false
127
+ * obj.get(1) // 42
128
+ * obj.count() // 1
129
+ */
80
130
```
81
131
82
132
### ** ...**
Original file line number Diff line number Diff line change @@ -68,7 +68,49 @@ At t=250, count() returns 0 because the cache is empty.
68
68
### ** TypeScript**
69
69
70
70
``` ts
71
-
71
+ class TimeLimitedCache {
72
+ private cache: Map <number , [value : number , expire : number ]> = new Map ();
73
+
74
+ constructor () {}
75
+
76
+ set(key : number , value : number , duration : number ): boolean {
77
+ this .removeExpire ();
78
+ const ans = this .cache .has (key );
79
+ this .cache .set (key , [value , this .now () + duration ]);
80
+ return ans ;
81
+ }
82
+
83
+ get(key : number ): number {
84
+ this .removeExpire ();
85
+ return this .cache .get (key )?.[0 ] ?? - 1 ;
86
+ }
87
+
88
+ count(): number {
89
+ this .removeExpire ();
90
+ return this .cache .size ;
91
+ }
92
+
93
+ private now(): number {
94
+ return new Date ().getTime ();
95
+ }
96
+
97
+ private removeExpire(): void {
98
+ const now = this .now ();
99
+ for (const [key, [, expire]] of this .cache ) {
100
+ if (expire <= now ) {
101
+ this .cache .delete (key );
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Your TimeLimitedCache object will be instantiated and called as such:
109
+ * var obj = new TimeLimitedCache()
110
+ * obj.set(1, 42, 1000); // false
111
+ * obj.get(1) // 42
112
+ * obj.count() // 1
113
+ */
72
114
```
73
115
74
116
### ** ...**
Original file line number Diff line number Diff line change
1
+ class TimeLimitedCache {
2
+ private cache : Map < number , [ value : number , expire : number ] > = new Map ( ) ;
3
+
4
+ constructor ( ) { }
5
+
6
+ set ( key : number , value : number , duration : number ) : boolean {
7
+ this . removeExpire ( ) ;
8
+ const ans = this . cache . has ( key ) ;
9
+ this . cache . set ( key , [ value , this . now ( ) + duration ] ) ;
10
+ return ans ;
11
+ }
12
+
13
+ get ( key : number ) : number {
14
+ this . removeExpire ( ) ;
15
+ return this . cache . get ( key ) ?. [ 0 ] ?? - 1 ;
16
+ }
17
+
18
+ count ( ) : number {
19
+ this . removeExpire ( ) ;
20
+ return this . cache . size ;
21
+ }
22
+
23
+ private now ( ) : number {
24
+ return new Date ( ) . getTime ( ) ;
25
+ }
26
+
27
+ private removeExpire ( ) : void {
28
+ const now = this . now ( ) ;
29
+ for ( const [ key , [ , expire ] ] of this . cache ) {
30
+ if ( expire <= now ) {
31
+ this . cache . delete ( key ) ;
32
+ }
33
+ }
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Your TimeLimitedCache object will be instantiated and called as such:
39
+ * var obj = new TimeLimitedCache()
40
+ * obj.set(1, 42, 1000); // false
41
+ * obj.get(1) // 42
42
+ * obj.count() // 1
43
+ */
You can’t perform that action at this time.
0 commit comments