-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApril-9-2022-k-most-frequent.js
97 lines (84 loc) · 2.18 KB
/
April-9-2022-k-most-frequent.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
class BH {
constructor() {
this.values = [];
}
add(element) {
this.values.push(element);
let index = this.values.length - 1;
const current = this.values[index];
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
let parent = this.values[parentIndex];
if (parent.value >= current.value) {
this.values[parentIndex] = current;
this.values[index] = parent;
index = parentIndex;
} else break;
}
}
pop() {
const max = this.values[0];
const end = this.values.pop();
this.values[0] = end;
let index = 0;
const length = this.values.length;
const current = this.values[0];
while (true) {
let leftChildIndex = 2 * index + 1;
let rightChildIndex = 2 * index + 2;
let leftChild, rightChild;
let swap = null;
if (leftChildIndex < length) {
leftChild = this.values[leftChildIndex];
if (leftChild.value < current.value) swap = leftChildIndex;
}
if (rightChildIndex < length) {
rightChild = this.values[rightChildIndex];
if (
(swap === null && rightChild.value < current.value) ||
(swap !== null && rightChild.value < leftChild.value)
)
swap = rightChildIndex;
}
if (swap === null) break;
this.values[index] = this.values[swap];
this.values[swap] = current;
index = swap;
}
return max;
}
}
var topKFrequent = function(nums, k) {
if ( nums.length === 1 ) return [...nums];
const map = new Map();
const heap = new BH();
for( const num of nums ) {
let count = map.get(num);
if ( ! count ) {
count = {
value: 0
};
map.set(num, count);
}
count.value += 1;
}
for( const [num, {value}] of map.entries() ) {
heap.add({value,num});
while(heap.values.length > k) {
heap.pop();
}
}
return heap.values.map(({num}) => num);
};
const T = [
topKFrequent([1,1,1,2,2,3], 2),
topKFrequent([1], 1),
topKFrequent([1,2,3,4,5,6,7,8,9,10], 10),
topKFrequent([1,2,2,2,3,3,4,5,6,7,4,4,8,9,4,10,4,11], 2),
];
console.log(T);