解析见:数据流中的中位数
/**
* initialize your data structure here.
*/
var MedianFinder = function() {
this.maxHeap = new Heap('max');
this.minHeap = new Heap('min');
this.count = 0;
};
/**
* @param {number} num
* @return {void}
*/
MedianFinder.prototype.addNum = function(num) {
this.count++;
if (this.count % 2 === 1) {
this.maxHeap.add(num);
this.minHeap.add(this.maxHeap.pop());
} else {
this.minHeap.add(num);
this.maxHeap.add(this.minHeap.pop());
}
};
/**
* @return {number}
*/
MedianFinder.prototype.findMedian = function() {
if (this.count === 0) {
return 0;
}
if (this.count % 2 === 1) {
return this.minHeap.value[0];
} else {
return (this.minHeap.value[0] + this.maxHeap.value[0]) / 2
}
};
function Heap(type = 'min') {
this.type = type;
this.value = [];
}
Heap.prototype.create = function () {
const length = this.value.length;
for (let i = Math.floor(length / 2) - 1; i >= 0; i--) {
this.ajust(i, length);
}
}
Heap.prototype.ajust = function (index, length) {
const array = this.value;
for (let i = 2 * index + 1; i < length; i = 2 * i + 1) {
if (i + 1 < length) {
if ((this.type === 'max' && array[i + 1] > array[i]) ||
(this.type === 'min' && array[i + 1] < array[i])) {
i++;
}
}
if ((this.type === 'max' && array[index] < [array[i]]) ||
(this.type === 'min' && array[index] > [array[i]])) {
[array[index], array[i]] = [array[i], array[index]];
index = i;
} else {
break;
}
}
}
Heap.prototype.add = function (element) {
const array = this.value;
array.push(element);
if (array.length > 1) {
let index = array.length - 1;
let target = Math.floor((index - 1) / 2);
while (target >= 0) {
if ((this.type === 'min' && array[index] < array[target]) ||
(this.type === 'max' && array[index] > array[target])) {
[array[index], array[target]] = [array[target], array[index]]
index = target;
target = Math.floor((index - 1) / 2);
} else {
break;
}
}
}
}
Heap.prototype.pop = function () {
const array = this.value;
let result = null;
if (array.length > 1) {
result = array[0];
array[0] = array.pop();
this.ajust(0, array.length);
} else if (array.length === 1) {
return array.pop();
}
return result;
}