LeetCode Find Median from Data Stream 堆

原创 2015年11月17日 20:33:33

思路:

参考DISCUSS。

通过大顶堆+小顶堆来实现。经典。

大顶堆中堆顶的元素如果小于小顶堆堆顶的元素,则大顶堆中的所有元素就都小于小顶堆中的所有元素。如果再保证maxHeap.size()==minHeap.size() 或者 maxHeap.size() == minHeap.size() + 1的话,就使用两个堆顶元素就可以直接计算出中位数。

所以得出:

  1. 如果maxHeap.size()==minHeap.size() ,(大顶堆堆顶元素+小顶堆堆顶元素)/2就是中位数;
  2. 如果maxHeap.size() == minHeap.size() + 1,大顶堆堆顶元素就是中位数;

java code:

class MedianFinder {

    PriorityQueue<Long> maxHeap;
    PriorityQueue<Long> minHeap;
    public MedianFinder() {
        maxHeap = new PriorityQueue<Long>(Collections.reverseOrder());//change to a maximum heap
        minHeap = new PriorityQueue<Long>();//heap is a minimal heap by default
    }
    // Adds a number into the data structure.
    public void addNum(int num) {
        maxHeap.add((long)num);
        minHeap.add(maxHeap.poll());
        if(maxHeap.size() < minHeap.size()) {
            maxHeap.add(minHeap.poll());
        }
    }

    // Returns the median of current data stream
    public double findMedian() {
        if(maxHeap.size() == minHeap.size()) {
            return (maxHeap.peek() + minHeap.peek()) / 2.0;
        }else {
            return maxHeap.peek();
        }
    }
};

// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Elem...

LeetCode295——Find Median from Data Stream

LeetCode295——Find Median from Data Stream 前提是Ordered List。 但是插入即addNum操作可能是无序的。 尝试过对序列排序再返回的办法结果是...

Leetcode 295. Find Median from Data Stream (Hard) (cpp)

Leetcode 295. Find Median from Data Stream (Hard) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月14日 00:54
  • 203

leetcode -- Find Median from Data Stream -- 设计题重点

https://leetcode.com/problems/find-median-from-data-stream/利用minHeap以及maxHeap。把一个ordered num list, 分...
  • xyqzki
  • xyqzki
  • 2015年12月25日 10:58
  • 311

[leetcode] 295. Find Median from Data Stream

题目:Median is the middle value in an ordered integer list. If the size of the list is even, there is ...

leetcode No295. Find Median from Data Stream

QuestionMedian is the middle value in an ordered integer list. If the size of the list is even, ther...

[leetcode] 295. Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

leetcode 295 : Find Median from Data Stream

1、原题如下: Median is the middle value in an ordered integer list. If the size of the list is even, the...

LeetCode Find Median from Data Stream

Description: Median is the middle value in an ordered integer list. If the size of the list is ...

leetcode 295. Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode Find Median from Data Stream 堆
举报原因:
原因补充:

(最多只允许输入30个字)