Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[fix][javascript] find-median-from-data-stream #1589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -24998,30 +24998,32 @@ class MedianFinder {
// by chatGPT (javascript)
var MedianFinder = function() {
// 小顶堆
var large = new PriorityQueue();
var large = new PriorityQueue({
compare: (a, b) => a - b
});
// 大顶堆
var small = new PriorityQueue(function(a, b) {
return b - a;
var small = new PriorityQueue({
compare: (a, b) => b - a
});

this.findMedian = function() {
// 如果元素不一样多,多的那个堆的堆顶元素就是中位数
if (large.size() < small.size()) {
return small.peek();
return small.front();
} else if (large.size() > small.size()) {
return large.peek();
return large.front();
}
// 如果元素一样多,两个堆堆顶元素的平均数是中位数
return (large.peek() + small.peek()) / 2.0;
return (large.front() + small.front()) / 2.0;
};

this.addNum = function(num) {
if (small.size() >= large.size()) {
small.offer(num);
large.offer(small.poll());
small.enqueue(num);
large.enqueue(small.dequeue());
} else {
large.offer(num);
small.offer(large.poll());
large.enqueue(num);
small.enqueue(large.dequeue());
}
};
};
Expand Down