Skip to content

Commit 5f60e89

Browse files
authored
refactor: update solution to lc problem: No.2622 (doocs#2836)
1 parent d14c5d9 commit 5f60e89

File tree

3 files changed

+49
-70
lines changed

3 files changed

+49
-70
lines changed

solution/2600-2699/2622.Cache With Time Limit/README.md

+16-23
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,32 @@ timeDelays = [0, 0, 40, 50, 120, 200, 250]
9797

9898
```ts
9999
class TimeLimitedCache {
100-
private cache: Map<number, [value: number, expire: number]> = new Map();
101-
102-
constructor() {}
100+
#cache: Map<number, [value: number, expire: number]> = new Map();
103101

104102
set(key: number, value: number, duration: number): boolean {
105-
this.removeExpire();
106-
const ans = this.cache.has(key);
107-
this.cache.set(key, [value, this.now() + duration]);
108-
return ans;
103+
const isExist = this.#cache.has(key);
104+
105+
if (!this.#isExpired(key)) {
106+
this.#cache.set(key, [value, Date.now() + duration]);
107+
}
108+
109+
return isExist;
109110
}
110111

111112
get(key: number): number {
112-
this.removeExpire();
113-
return this.cache.get(key)?.[0] ?? -1;
113+
if (this.#isExpired(key)) return -1;
114+
const res = this.#cache.get(key)?.[0] ?? -1;
115+
return res;
114116
}
115117

116118
count(): number {
117-
this.removeExpire();
118-
return this.cache.size;
119+
const xs = Array.from(this.#cache).filter(([key]) => !this.#isExpired(key));
120+
return xs.length;
119121
}
120122

121-
private now(): number {
122-
return new Date().getTime();
123-
}
124-
125-
private removeExpire(): void {
126-
const now = this.now();
127-
for (const [key, [, expire]] of this.cache) {
128-
if (expire <= now) {
129-
this.cache.delete(key);
130-
}
131-
}
132-
}
123+
#isExpired = (key: number) =>
124+
this.#cache.has(key) &&
125+
(this.#cache.get(key)?.[1] ?? Number.NEGATIVE_INFINITY) < Date.now();
133126
}
134127

135128
/**

solution/2600-2699/2622.Cache With Time Limit/README_EN.md

+17-24
Original file line numberDiff line numberDiff line change
@@ -81,47 +81,40 @@ At t=250, count() returns 0 because the cache is empty.
8181

8282
<!-- solution:start -->
8383

84-
### Solution 1
84+
### Solution 1: Hash Table
8585

8686
<!-- tabs:start -->
8787

8888
#### TypeScript
8989

9090
```ts
9191
class TimeLimitedCache {
92-
private cache: Map<number, [value: number, expire: number]> = new Map();
93-
94-
constructor() {}
92+
#cache: Map<number, [value: number, expire: number]> = new Map();
9593

9694
set(key: number, value: number, duration: number): boolean {
97-
this.removeExpire();
98-
const ans = this.cache.has(key);
99-
this.cache.set(key, [value, this.now() + duration]);
100-
return ans;
95+
const isExist = this.#cache.has(key);
96+
97+
if (!this.#isExpired(key)) {
98+
this.#cache.set(key, [value, Date.now() + duration]);
99+
}
100+
101+
return isExist;
101102
}
102103

103104
get(key: number): number {
104-
this.removeExpire();
105-
return this.cache.get(key)?.[0] ?? -1;
105+
if (this.#isExpired(key)) return -1;
106+
const res = this.#cache.get(key)?.[0] ?? -1;
107+
return res;
106108
}
107109

108110
count(): number {
109-
this.removeExpire();
110-
return this.cache.size;
111+
const xs = Array.from(this.#cache).filter(([key]) => !this.#isExpired(key));
112+
return xs.length;
111113
}
112114

113-
private now(): number {
114-
return new Date().getTime();
115-
}
116-
117-
private removeExpire(): void {
118-
const now = this.now();
119-
for (const [key, [, expire]] of this.cache) {
120-
if (expire <= now) {
121-
this.cache.delete(key);
122-
}
123-
}
124-
}
115+
#isExpired = (key: number) =>
116+
this.#cache.has(key) &&
117+
(this.#cache.get(key)?.[1] ?? Number.NEGATIVE_INFINITY) < Date.now();
125118
}
126119

127120
/**

solution/2600-2699/2622.Cache With Time Limit/Solution.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
11
class TimeLimitedCache {
2-
private cache: Map<number, [value: number, expire: number]> = new Map();
3-
4-
constructor() {}
2+
#cache: Map<number, [value: number, expire: number]> = new Map();
53

64
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;
5+
const isExist = this.#cache.has(key);
6+
7+
if (!this.#isExpired(key)) {
8+
this.#cache.set(key, [value, Date.now() + duration]);
9+
}
10+
11+
return isExist;
1112
}
1213

1314
get(key: number): number {
14-
this.removeExpire();
15-
return this.cache.get(key)?.[0] ?? -1;
15+
if (this.#isExpired(key)) return -1;
16+
const res = this.#cache.get(key)?.[0] ?? -1;
17+
return res;
1618
}
1719

1820
count(): number {
19-
this.removeExpire();
20-
return this.cache.size;
21+
const xs = Array.from(this.#cache).filter(([key]) => !this.#isExpired(key));
22+
return xs.length;
2123
}
2224

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-
}
25+
#isExpired = (key: number) =>
26+
this.#cache.has(key) &&
27+
(this.#cache.get(key)?.[1] ?? Number.NEGATIVE_INFINITY) < Date.now();
3528
}
3629

3730
/**

0 commit comments

Comments
 (0)