-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
82 lines (75 loc) · 2.39 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class AllOne {
#count_to_strings: Record<string | number, Set<string> | undefined> = {};
#string_to_count = new Map<string, number>();
inc(key: string): void {
const count = this.#string_to_count.get(key) || 0;
if (this.#string_to_count.has(key) && count) {
this.#string_to_count.set(key, count + 1);
} else {
this.#string_to_count.set(key, 1);
}
this.#count_to_strings[count]?.delete(key);
let set = this.#count_to_strings[count + 1];
if (!set) {
set = new Set<string>();
this.#count_to_strings[count + 1] = set;
}
set.add(key);
if (this.#count_to_strings[count]?.size === 0) {
Reflect.deleteProperty(this.#count_to_strings, count);
}
}
dec(key: string): void {
const count = this.#string_to_count.get(key) || 0;
if (count === 0) {
return;
}
if (this.#string_to_count.has(key) && count) {
this.#string_to_count.set(key, count - 1);
}
if (count === 1) this.#string_to_count.delete(key);
this.#count_to_strings[count]?.delete(key);
if (count - 1 > 0) {
let set = this.#count_to_strings[count - 1];
if (!set) {
set = new Set<string>();
this.#count_to_strings[count - 1] = set;
}
set.add(key);
}
if (this.#count_to_strings[count]?.size === 0) {
Reflect.deleteProperty(this.#count_to_strings, count);
}
}
getMaxKey(): string {
const num = Object.keys(this.#count_to_strings).at(-1);
if (typeof num === "undefined") return "";
const set = this.#count_to_strings[num];
if (set) {
for (const key of set) {
return key;
}
}
return "";
}
getMinKey(): string {
const num = Object.keys(this.#count_to_strings).at(0);
if (typeof num === "undefined") return "";
const set = this.#count_to_strings[num];
if (set) {
for (const key of set) {
return key;
}
}
return "";
}
}
export default AllOne;
/**
* Your AllOne object will be instantiated and called as such:
* var obj = new AllOne()
* obj.inc(key)
* obj.dec(key)
* var param_3 = obj.getMaxKey()
* var param_4 = obj.getMinKey()
*/