leetcode@ [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
 class MedianFinder {
 private:
     priority_queue<int,vector<int>, greater<int> > maxHeap;
     priority_queue<int> minHeap;
 public:
     // Adds a number into the data structure.
     void addNum(int num) {
          if(minHeap.empty() || num <= minHeap.top()){
              if(minHeap.size() > maxHeap.size()){
                  maxHeap.push(minHeap.top());
                  minHeap.pop();
              }
              minHeap.push(num);
          }
          else if(maxHeap.empty() || num > maxHeap.top()){
              if(maxHeap.size() > minHeap.size()){
                  minHeap.push(maxHeap.top());
                  maxHeap.pop();
              }
              maxHeap.push(num);
          }
          else{
              if(maxHeap.size() >= minHeap.size()) minHeap.push(num);
              else if(minHeap.size() > maxHeap.size()) maxHeap.push(num);
          }
     }
     // Returns the median of current data stream
     double findMedian() {
         if(minHeap.size() == maxHeap.size()) return (double) (minHeap.top() + maxHeap.top()) / 2.0;
         else if(minHeap.size() > maxHeap.size()) return (double) minHeap.top();
         else return (double) maxHeap.top();
     }
 };
 // Your MedianFinder object will be instantiated and called as such:
 // MedianFinder mf;
 // mf.addNum(1);
 // mf.findMedian();
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 https://leetcode.co ... 
- [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数据流的中位数
		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
		一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ... 
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
		注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ... 
- 【LeetCode】295. Find Median from Data Stream 解题报告(C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ... 
- 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 ... 
- [LC] 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 ... 
- 295 Find Median from Data Stream 数据流的中位数
		中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ... 
随机推荐
- [BEC][hujiang] Lesson04 Unit1:Working life ---Reading + Listening &Grammar & Speaking
			4 1.1 Working life P10 Reading----The anonymous CV Exercise 3 What should be included in the CV ... 
- Creating a new Signiant Transfer Engine because the previous transfer had to be canceled.
			From: http://stackoverflow.com/questions/10548196/application-loader-new-weird-warning-about-signian ... 
- jvisualvm 使用
			和jconsole侧重于内存分析和检测不同,jvisualvm在线程分析方面更强大一些,下面简单介绍下使用: 1. 在要监控的java应用配置文件中,本例是apache-jmeter/bin/jmet ... 
- 关于DataTables一些小结
			最近项目中使用了DataTables,故小结了一下. 导入CSS文件<link rel="stylesheet" href="<%=base %>/js ... 
- 214. Shortest Palindrome
			题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ... 
- SOCKS5协议
			SOCKS5 是一个代理协议,这种协议对本身所代理的内容并不关心,可用于穿越防火墙. 例如我有一台web服务器,用户可以登陆上去查询公司的关键数据,这样的服务器我肯定是不想放到公网上让别人能随便访问, ... 
- 从零开始定义自己的JavaScript框架(一)
			来自:http://www.ituring.com.cn/article/48461 1.1 模块的定义 一个框架想要能支撑较大的应用,首先要考虑怎么做模块化.有了内核和模块加载系统,外围的模块就可以 ... 
- python函数--传参
			一.位置参数 二.关键字参数 三.默认参数 四.任意数量的位置参数: 1. *x 2. 传入的多个参数,最终合并成一个元组 3. 之后的参数必须用关键字参数 五.任意数量的关键字参数 1. **x 2 ... 
- PHP上传遇到的问题-php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项
			今天在做上传的时候出现一个非常怪的问题,有时候表单提交可以获取到值,有时候就获取不到了,连普通的字段都获取不到了,苦思冥想还没解决,最后问了师傅,师傅看了说挺奇怪的,然后问我upload_max_fi ... 
- JavaScript DOM高级程序设计 2.4-try{}catch{}--我要坚持到底!
			先看一段有异常的语句 var sound = 'Roar!'; function myOrneryBeast() { this.style.color='green';//window没有style属 ... 
