-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path347_top_k_frequent_elements.js
91 lines (81 loc) · 2.52 KB
/
347_top_k_frequent_elements.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
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
// Step 1: Count the frequency of each element using a hashmap
const freqMap = new Map();
for (let num of nums) {
freqMap.set(num, (freqMap.get(num) || 0) + 1);
}
// Step 2: Create a min heap to store the k most frequent elements
const minHeap = new MinHeap();
for (let [num, freq] of freqMap.entries()) {
minHeap.push({ num, freq });
if (minHeap.size() > k) {
minHeap.pop();
}
}
// Step 3: Extract the elements from the min heap
const result = [];
while (!minHeap.isEmpty()) {
result.unshift(minHeap.pop().num);
}
return result;
};
class MinHeap {
constructor() {
this.heap = [];
}
push(value) {
this.heap.push(value);
this.heapifyUp();
}
pop() {
if (this.isEmpty()) return null;
const minElement = this.heap[0];
this.heap[0] = this.heap[this.heap.length - 1];
this.heap.pop();
this.heapifyDown();
return minElement;
}
size() {
return this.heap.length;
}
isEmpty() {
return this.heap.length === 0;
}
heapifyUp() {
let currentIdx = this.heap.length - 1;
while (currentIdx > 0) {
const parentIdx = Math.floor((currentIdx - 1) / 2);
if (this.heap[currentIdx].freq < this.heap[parentIdx].freq) {
[this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]];
currentIdx = parentIdx;
} else {
break;
}
}
}
heapifyDown() {
let currentIdx = 0;
while (currentIdx < this.heap.length) {
let minIdx = currentIdx;
const leftChildIdx = currentIdx * 2 + 1;
const rightChildIdx = currentIdx * 2 + 2;
if (leftChildIdx < this.heap.length && this.heap[leftChildIdx].freq < this.heap[minIdx].freq) {
minIdx = leftChildIdx;
}
if (rightChildIdx < this.heap.length && this.heap[rightChildIdx].freq < this.heap[minIdx].freq) {
minIdx = rightChildIdx;
}
if (minIdx !== currentIdx) {
[this.heap[currentIdx], this.heap[minIdx]] = [this.heap[minIdx], this.heap[currentIdx]];
currentIdx = minIdx;
} else {
break;
}
}
}
}