deno.land / x / masx200_leetcode_test@10.6.5 / all-oone-data-structure / 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
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;
masx200_leetcode_test

Version Info

Tagged at
a year ago