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设计一个 ...
随机推荐
- 【BZOJ 1045】 1045: [HAOI2008] 糖果传递
1045: [HAOI2008] 糖果传递 Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1. Input 第一行一个正整数n& ...
- [Unity菜鸟] 摄像机视角控制
1. 摄像机预览物体 上下左右远近 把CameraFollow脚本赋给Camera,把要观察的对象赋给target using UnityEngine; using System.Collection ...
- 你不知道的pogo pin连接器
pogo pin连接器是一种带弹簧的探针式连接器,pogo pin连接器结构看起来非常简单,但其制造工艺要求极其的精细与复杂,从车床加工,电镀,组装等每道工序,如果没有一个有良好品质控制和完善的制造水 ...
- HDU1312——Red and Black(DFS)
Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...
- android 自动调整屏幕分辨率
请看 http://blog.csdn.net/awp258/article/details/7593340
- Can't obtain the input stream from /docProps/app.xml
今天在做poi修改样式时,报了以下错误: Exception in thread "main" org.apache.poi.POIXMLException: java.io.IO ...
- ,net运行框架
.NET FrameWork框架 是一套应用程序开发框架,主要目的提供一个开发模型. 主要的两个组件: 公共语言运行时(Common Language Runtime)(CLR): 提供内存管理.线程 ...
- HDU 1533 Going Home (最小费用流)
题意: 一个矩阵n*m,其中有k个房子和k个人,k个人分别必须走到任意一个房子中(匹配),但是权值就是长度(非欧拉距离),求匹配完的权之和. 思路: 建图方法是,首先将k个人和k个房子分别抽出来到集合 ...
- hdu 2844 Coins
Coins Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted S ...
- Rman实现数据库迁移
Rman实现数据库迁移(从库A迁移到库B)环境:服务器A:Oracle10g+AS3服务器B:Oracle10g+AS4准备工作: 1 在数据库B上建立与库A相同的目录结构(若由于磁盘空间等原因可以用 ...