[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 (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 private:
 3     multiset<int> left, right;
 4
 5 public:
 6
 7     // Adds a number into the data structure.
 8     void addNum(int num) {
 9         if (right.empty() || num < *right.begin()) left.insert(num);
10         else right.insert(num);
11     }
12
13     // Returns the median of current data stream
14     double findMedian() {
15         int size = left.size() + right.size();
16         while (left.size() > size / 2) {
17             int tmp = *left.rbegin();
18             right.insert(tmp);
19             left.erase(left.find(tmp));
20         }
21         while (left.size() < size / 2) {
22             int tmp = *right.begin();
23             left.insert(tmp);
24             right.erase(right.find(tmp));
25         }
26         if (size & 1) return *right.begin();
27         else return (double)(*left.rbegin() + *right.begin()) / 2;
28     }
29 };
30
31 // Your MedianFinder object will be instantiated and called as such:
32 // MedianFinder mf;
33 // mf.addNum(1);
34 // mf.findMedian();
时间: 2024-12-24 06:14:11

[LeetCode] Find Median from Data Stream的相关文章

[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 Design a d

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

LeetCode之Find Median from Data Stream

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

数据结构与算法(1)支线任务8——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] 

[email&#160;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 m

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向数列中添

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

Find Median from Data Stream

常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector<int>& coll,int idx,int max){ int left=2*idx+1,right=2*idx+2; int largest=idx; if(left<max&&coll[left]>coll[idx]) largest=left; if(rig

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