[leetcode]295. Find Median from Data Stream?????????????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]295. Find Median from Data Stream?????????????????????相关的知识,希望对你有一定的参考价值。
?????????you following media vat balance 2.0 find design ??????
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.
For example,
[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:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
Example:
addNum(1) addNum(2) findMedian() -> 1.5 addNum(3) findMedian() -> 2
?????????
????????????????????????????????????(????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????)
?????????
????????????????????????????????????????????????????????????
???PriorityQueue?????????maxHeap, minHeap, ?????????maxHeap.size() - minHeap.size() <=1
??????????????????maxHeap.size() = minHeap.size() ????????????????????????????????????Heap???peek???????????????
?????????maxHeap.size() > minHeap.size() ??????????????????????????????maxHeap.peek
???????????????????????????
[6,??????10,??????2,??????6,??????5,??????0,??????6,??????3]
^ ???maxHeap.isEmpty() || ???????????? < maxHeap.peek() ??? ??????maxHeap??????
[6,??????10,??????2,??????6,??????5,??????0,??????6,??????3]
^ ??????????????? >= maxHeap.peek() ??? ??????minHeap??????
[6,??????10,??????2,??????6,??????5,??????0,??????6,??????3]
^ ???????????? < maxHeap.peek() ??? ??????maxHeap??????
[6,??????10,??????2,??????6,??????5,??????0,??????6,??????3]
^ ??????????????? >= maxHeap.peek() ??? ??????minHeap?????????minHeap???????????????????????????sort???peek??????
[6,??????10,??????2,??????6,??????5,??????0,??????6,??????3]
^ ???????????? < maxHeap.peek() ??? ??????maxHeap??????
??????maxHeap.size() - minHeap.size() = 2 ??????????????????
???maxHeap.peek??????minHeap??????????????????
......
??????????????????????????????????????????
?????????
1 class MedianFinder { 2 3 /** initialize your data structure here. */ 4 private PriorityQueue<Integer> _maxHeap; 5 private PriorityQueue<Integer> _minHeap; 6 7 public MedianFinder() { 8 _maxHeap = new PriorityQueue<> ((o1,o2) -> o2-o1); 9 _minHeap = new PriorityQueue<> ((o1,o2) -> o1-o2); 10 } 11 12 public void addNum(int num) { 13 // put each integer into either maxHeap or minHeap 14 if(_maxHeap.isEmpty() || num < _maxHeap.peek()){ 15 _maxHeap.add(num); 16 } else{ 17 _minHeap.add(num); 18 } 19 // keep balance 20 if(_maxHeap.size() ==_minHeap.size()+2){ 21 _minHeap.add(_maxHeap.poll()); 22 } 23 24 if(_minHeap.size() ==_maxHeap.size()+1){ 25 _maxHeap.add(_minHeap.poll()); 26 } 27 28 } 29 // the number of data is even or odd 30 public double findMedian() { 31 if (_maxHeap.size() == _minHeap.size()) { 32 return (_maxHeap.peek() + _minHeap.peek()) / 2.0; 33 }else { 34 return _maxHeap.peek(); 35 } 36 } 37 } 38 39 /** 40 * Your MedianFinder object will be instantiated and called as such: 41 * MedianFinder obj = new MedianFinder(); 42 * obj.addNum(num); 43 * double param_2 = obj.findMedian(); 44 */
以上是关于[leetcode]295. Find Median from Data Stream?????????????????????的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode——295. Find Median from Data Stream
295. Find Median from Data Stream
295 Find Median from Data Stream