2014-04-29 04:18

题目:有一连串的数被读入,设计一个数据结构,能随时返回当前所有数的中位数。

解法:用一个大顶堆,一个小顶堆将数分成数量最接近的两份,就能轻松得到中位数了。

代码:

 // 18.9 A stream of integers are passed to you, you have to tell me the median as they keep coming in.
#include <climits>
#include <iostream>
#include <vector>
#include <queue>
using namespace std; template <class T>
struct LessFunctor
{
bool operator() (const T &x, const T &y)
{
return x < y;
}
}; template <class T>
struct GreaterFunctor
{
bool operator() (const T &x, const T &y)
{
return x > y;
}
}; template <class T>
class MedianArray {
public:
MedianArray() {
n_small = ;
n_great = ;
}; void push(const T& val) {
if (n_great == ) {
greater_heap.push(val);
++n_great;
return;
} if (n_great > n_small) {
smaller_heap.push(val);
++n_small;
} else {
greater_heap.push(val);
++n_great;
} if (greater_heap.top() < smaller_heap.top()) {
T tmp; tmp = greater_heap.top();
greater_heap.pop();
greater_heap.push(smaller_heap.top());
smaller_heap.pop();
smaller_heap.push(tmp);
}
}; T median() {
if (n_great == ) {
return INT_MIN;
} else if (n_great > n_small) {
return greater_heap.top();
} else {
return (smaller_heap.top() + greater_heap.top()) / ;
}
}; ~MedianArray() {
n_small = ;
n_great = ;
while (!greater_heap.empty()) {
greater_heap.pop();
}
while (!smaller_heap.empty()) {
smaller_heap.pop();
}
};
private:
int n_small; // greater elements are stored in here.
priority_queue<T, vector<T>, GreaterFunctor<T> > greater_heap; int n_great; // smaller elements are stored in here.
priority_queue<T, vector<T>, LessFunctor<T> > smaller_heap;
}; int main()
{
MedianArray<int> ma;
int val; while (cin >> val) {
ma.push(val);
cout << ma.median() << endl;
} return ;
}

《Cracking the Coding Interview》——第18章:难题——题目9的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

随机推荐

  1. Python 类的高级属性(可选)

    1.slots实例:限制类的实例有合法的属性集,只有__slots__属性列表中的属性才可能成为实例属性. 对象的实例通常没有一个属性字典,可以在__slots__列表中包含一个属性字典__dict_ ...

  2. RPC电源监控总结

    首先说一下监控机箱的监控原理. 设备的信息传输是通过tcp或者udp传输十六进制的数然后进行解析,传输数据. 如图: 设备反馈信息也是返回来的十六机制,然后按照对应的位置进将数据解析成二进制,用二进制 ...

  3. MySQL latch小结

      lock和latch的比较 对于INNODB存储引擎中的latch可以通过命令 SHOW ENGINE INNODB MUTEX 看到latch的更多信息 说明: 列Type显示的总是 InnoD ...

  4. selenium 打开浏览器报错java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/String;)

    java.lang.NoSuchMethodError: org.openqa.selenium.chrome.ChromeOptions.addArguments([Ljava/lang/Strin ...

  5. hdu-3015 Disharmony Trees---离散化+两个树状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的ra ...

  6. CentOS6.5 配置IP的两种方式

    1.dhcp动态获取ip 编辑配置文件 /etc/sysconfig/network-scripts/ifcfg-eth0 ,配置如下: [root@localhost ~]# vi /etc/sys ...

  7. linux下jdk的安装配置

    1.下载jdk:地址 选中你选择的版本,下载linux版本对应你系统的32位或64位. 我这里选择的是64位. 2.使用你的ssh直连工具把安装包丢到/usr/local/目录下 3.解压安装jdk ...

  8. XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历

    本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书 ...

  9. hdu_1452_Happy 2004 (乘法逆元

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

  10. selenium等待页面加载完成

    https://blog.csdn.net/hu_zhenghui/article/details/77429505 38行      这种方法 不准确   还在空白页时候   就会 返回  comp ...