Skip to content

Commit

Permalink
fix(javascript): top-k-frequent-elements
Browse files Browse the repository at this point in the history
  • Loading branch information
wangruilong committed Apr 24, 2024
1 parent 4f44711 commit 7f32fa8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -63975,18 +63975,21 @@ var topKFrequent = function(nums, k) {
valToFreq.set(v, valToFreq.get(v) ? valToFreq.get(v) + 1: 1);
}

const pq = new PriorityQueue((a, b) => a.value < b.value);
// 最小堆
const pq = new PriorityQueue({
compare: (a, b) => a.value - b.value
});

for (let [key, value] of valToFreq) {
pq.offer({key, value});
pq.enqueue({key, value});
if (pq.size() > k) {
pq.poll();
pq.dequeue();
}
}

const res = [];
for (let i = 0; i < k; i++) {
res.push(pq.poll().key);
res.push(pq.dequeue().key);
}
return res;
};
Expand Down

0 comments on commit 7f32fa8

Please sign in to comment.