题目:
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:[2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
For example:
add(1) add(2) findMedian() -> 1.5 add(3) findMedian() -> 2题解:
练习一下三种语言的最小堆和最大堆写法。目前看来同时需要两个堆,最方便的是Java。Python版超时。
C++版:
struct Min_Heap { Min_Heap(int n): _n(n) {} operator int() { return _n;} int _n; }; bool operator < (Min_Heap l, Min_Heap r) { return l._n > r._n; } class MedianFinder { public: // Adds a number into the data structure. void addNum(int num) { if(minHeap.size() > 0 && num > minHeap.top()._n) minHeap.push(num); else if(maxHeap.size() > 0 && num < maxHeap.top()) maxHeap.push(num); else minHeap.push(num); if((int)minHeap.size() - (int)maxHeap.size() > 1) { int topMin = minHeap.top()._n; minHeap.pop(); maxHeap.push(topMin); } else if((int)maxHeap.size() - (int)minHeap.size() > 1) { int topMax = maxHeap.top(); maxHeap.pop(); minHeap.push(topMax); } } // Returns the median of current data stream double findMedian() { if(maxHeap.size() == minHeap.size()) { return (double)(maxHeap.top() + minHeap.top()._n) / 2; } else if(maxHeap.size() > minHeap.size()){ return (double) maxHeap.top(); } else { return (double) minHeap.top()._n; } } private: priority_queue<int> maxHeap; priority_queue<Min_Heap> minHeap; };
import java.util.*; class MedianFinder { // Adds a number into the data structure. public void addNum(int num) { if(minHeap.size() > 0 && num > minHeap.peek()) minHeap.add(num); else if(maxHeap.size() > 0 && num < maxHeap.peek()) maxHeap.add(num); else minHeap.add(num); if(minHeap.size() - maxHeap.size() > 1) { maxHeap.add(minHeap.poll()); } else if(maxHeap.size() - minHeap.size() > 1) { minHeap.add(maxHeap.poll()); } } // Returns the median of current data stream public double findMedian() { if(minHeap.size() > maxHeap.size()) return (double)minHeap.peek(); else if(minHeap.size() < maxHeap.size()) return (double)maxHeap.peek(); else return (double)(minHeap.peek() + maxHeap.peek()) / 2; } private PriorityQueue<Integer> minHeap = new PriorityQueue<>(); private PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); }; // Your MedianFinder object will be instantiated and called as such: // MedianFinder mf = new MedianFinder(); // mf.addNum(1); // mf.findMedian();
import heapq class MedianFinder: def __init__(self): """ Initialize your data structure here. """ self.minHeap = [] self.maxHeap = [] heapq.heapify(self.minHeap) heapq._heapify_max(self.maxHeap) def addNum(self, num): """ Adds a num into the data structure. :type num: int :rtype: void """ if len(self.minHeap) > 0 and num > self.minHeap[0]: heapq.heappush(self.minHeap, num) elif len(self.maxHeap) > 0 and num < heapq.nlargest(1, self.maxHeap)[0]: heapq.heappush(self.maxHeap, num) else: heapq.heappush(self.minHeap, num) if len(self.minHeap) - len(self.maxHeap) > 1: heapq.heappush(self.maxHeap, heapq.heappop(self.minHeap)) elif len(self.maxHeap) - len(self.minHeap) > 1: heapq.heappush(self.minHeap, heapq.heappop(self.maxHeap)) def findMedian(self): """ Returns the median of current data stream :rtype: float """ if len(self.minHeap) > len(self.maxHeap): return float(self.minHeap[0]) elif len(self.maxHeap) > len(self.minHeap): return float(heapq.nlargest(1, self.maxHeap)[0]) else: return float((self.minHeap[0] + heapq.nlargest(1, self.maxHeap)[0]) / 2) # Your MedianFinder object will be instantiated and called as such: # mf = MedianFinder() # mf.addNum(1) # mf.findMedian()