中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
例如,
[2,3,4] 的中位数是 3
[2,3] 的中位数是 (2 + 3) / 2 = 2.5
设计一个支持以下两种操作的数据结构:
- void addNum(int num) - 从数据流中添加一个整数到数据结构中。
- double findMedian() - 返回目前所有元素的中位数。
示例:
addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
思路:优先队列的思想,本文未使用C++ STL的priority_queue,而是动手实现了优先队列,感兴趣的可做参考!
所谓优先队列,本质上是队列,但不同之处在于优先队列顶元素是集合的最值(最大值对应大顶堆,最小值对应小顶堆),同时可以进行堆排序。
本题采用双堆方法,原则:
1. 大顶堆的顶元素要小于小顶堆的顶元素
2. 大顶堆里的元素数目与小顶堆的元素数目最多相差一个
当寻找数据流中的中值时,如果大顶堆与小顶堆的大小相同,即数据流中有偶数个数据时,两个堆顶元素的均值即为数据流的中值;如果两个堆的大小不同,即数据流中有奇数个数据时,元素数目多的那个堆所对应的堆顶元素即为数据流的中值。
实际上,大顶堆里的元素全都小于小顶堆里的元素,感觉有点像二分法,大顶堆与小顶堆里的元素按一定规则分布(有一定的顺序),同时堆顶元素或两堆顶元素的均值对应中值。
//大顶堆的实现
class priority_queueII
{
public:
priority_queueII()
{
nums = vector<int>(initCap);
}
void push(int x)
{
nums[count] = x;
int curIndex = count;
int rootIndex = 0;
while (curIndex > 0)
{
if (curIndex % 2 != 0)
{
rootIndex = curIndex / 2.0;
}
else
{
rootIndex = curIndex / 2.0 - 1;
}
if (nums[rootIndex] < nums[curIndex])
{
swap(nums[rootIndex], nums[curIndex]);
}
curIndex = rootIndex;
}
++count;
}
void maxHeap(int root, int n)
{
int left = 2 * root + 1;
if (left < n)
{
int max = left;
int right = 2 * root + 2;
if (right < n)
{
if (nums[right] > nums[left])
{
max = right;
}
}
if (nums[root] < nums[max])
{
swap(nums[root], nums[max]);
maxHeap(max, n);
}
}
}
bool pop()
{
if (count == 0) {
return false;
}
swap(nums[0], nums[count-1]);
--count;
maxHeap(0, count);
return true;
}
int size()
{
return count;
}
int top()
{
if (count == 0) {
return -1;
}
return nums[0];
}
public:
vector<int> nums;
const static int initCap = 10000;
int count = 0;
};
小顶堆的代码:
class priority_queueI
{
public:
priority_queueI()
{
nums = vector<int>(initCap);
}
void push(int x)
{
nums[count] = x;
int curIndex = count;
int rootIndex = 0;
while ( curIndex > 0)
{
if (curIndex % 2 != 0){
rootIndex = curIndex / 2;
}
else{
rootIndex = curIndex / 2 - 1;
}
if (nums[rootIndex] > nums[curIndex])
{
swap(nums[rootIndex], nums[curIndex]);
}
curIndex = rootIndex;
}
++count;
}
void minHeap(int root, int n)
{
int left = 2 * root + 1;
if (left < n)
{
int right = 2 * root + 2;
int minIndex = left;
if (right < n)
{
if (nums[right] < nums[left])
{
minIndex = right;
}
}
if (nums[root] > nums[minIndex])
{
swap(nums[root], nums[minIndex]);
minHeap(minIndex, n);
}
}
}
bool pop()
{
if (count == 0)
{
return false;
}
swap(nums[0], nums[count-1]); // 交换第一个元素和最后一个元素
--count; //然后数组的大小减小1,即相当于删除了顶元素
minHeap(0, count); //维持小顶堆的性质
return true;
}
int size()
{
return count;
}
int top()
{
if (count == 0)
{
return -1;
}
return nums[0];
}
public:
vector<int> nums;
int count = 0;
const static int initCap = 10000;
};
leetcode 295的实现代码:
class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() {
m_minHeap = new priority_queueI();
m_maxHeap = new priority_queueII();
}
void addNum(int num) {
if (count == 0)
{
m_minHeap->push(num);
++count;
return;
}
if (num > m_minHeap->top())
{
m_minHeap->push(num);
if (m_minHeap->size() - m_maxHeap->size() > 1){
m_maxHeap->push(m_minHeap->top());
m_minHeap->pop();
}
}
else
{
m_maxHeap->push(num);
if (m_maxHeap->size() - m_minHeap->size() > 1)
{
m_minHeap->push(m_maxHeap->top());
m_maxHeap->pop();
}
}
++count;
}
double findMedian() {
//创建大顶堆,小顶堆效率相对最大栈最小栈要差一些
if (m_minHeap->size() == m_maxHeap->size())
{
return (m_minHeap->top() + m_maxHeap->top()) / 2.0;
}
else if (m_minHeap->size() > m_maxHeap->size())
{
return m_minHeap->top();
}
else
{
return m_maxHeap->top();
}
}
public:
vector<int> nums;
priority_queueI *m_minHeap;
priority_queueII *m_maxHeap;
int count = 0;
};