[email protected] [295]Find Median from Data Stream

https://leetcode.com/problems/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 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:

  • 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.

For example:

add(1)
add(2)
findMedian() -> 1.5
add(3)
findMedian() -> 2
 1 class MedianFinder {
 2
 3 private:
 4     priority_queue<int,vector<int>, greater<int> > maxHeap;
 5     priority_queue<int> minHeap;
 6
 7 public:
 8
 9     // Adds a number into the data structure.
10     void addNum(int num) {
11          if(minHeap.empty() || num <= minHeap.top()){
12              if(minHeap.size() > maxHeap.size()){
13                  maxHeap.push(minHeap.top());
14                  minHeap.pop();
15              }
16              minHeap.push(num);
17          }
18          else if(maxHeap.empty() || num > maxHeap.top()){
19              if(maxHeap.size() > minHeap.size()){
20                  minHeap.push(maxHeap.top());
21                  maxHeap.pop();
22              }
23              maxHeap.push(num);
24          }
25          else{
26              if(maxHeap.size() >= minHeap.size()) minHeap.push(num);
27              else if(minHeap.size() > maxHeap.size()) maxHeap.push(num);
28          }
29     }
30
31     // Returns the median of current data stream
32     double findMedian() {
33         if(minHeap.size() == maxHeap.size()) return (double) (minHeap.top() + maxHeap.top()) / 2.0;
34         else if(minHeap.size() > maxHeap.size()) return (double) minHeap.top();
35         else return (double) maxHeap.top();
36     }
37 };
38
39 // Your MedianFinder object will be instantiated and called as such:
40 // MedianFinder mf;
41 // mf.addNum(1);
42 // mf.findMedian();
时间: 2024-10-10 07:02:23

[email protected] [295]Find Median from Data Stream的相关文章

解题报告: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 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 Desi

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 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 d

295 Find Median from Data Stream 数据流的中位数

中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个支持以下两种操作的数据结构:    void addNum(int num) - 从数据流中增加一个整数到数据结构中.    double findMedian() - 返回目前所有元素的中位数.例如:addNum(1)addNum(2)findMedian() -> 1.5addNum(3) fi

LeetCode——295. Find Median from Data Stream

一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据流是动态变化的.如果数据流中的数字个数是奇数的话,则中位数是中间位置的数字:如果数据流中的数字是偶数的话,则中位数是排序好的数据流中的中间两个数的的平均值. 三.题解: 如果数据流是静态不变的话,此时问题是比较好求解的.但是数据流是动态变化的,所以数据流中每次进入一个新的数字时,都要保证能够高效的找

[LeetCode] Find Median from Data Stream

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 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

Find Median from Data Stream 295

题目链接:https://leetcode.com/problems/find-median-from-data-stream/ 题目描述: 设计一个类,包含两种操作 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. 操作 addNum向数列中添

leetcode笔记: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 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 De

支线任务8-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 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 Des

LeetCode之Find Median from Data Stream

1.题目描述 翻译过来就是对输入的数字找出其中位数. 2.解题思路 一开始,我想着这是动态输入,所以必须放弃数组这些静态的数据结构.首先想到了平衡二叉树,然而太麻烦了.后面又想到了大顶堆以及小顶堆,但是不知如何运用,就上这道题discuss瞅了瞅.结果发现了一种double heap解法,十分带感,总体思路就是比中位数大的数字放一边(大数堆),小的放另一边(小数堆).然后也接触了并且运用一种新的队列用法——优先队列.插入数字过程中,始终保持大数堆大小大于等于小数堆,以便最后输出中位数.代码如下: