《Cracking the Coding Interview》——第18章:难题——题目9
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- 如何让.NET Core应用的配置与源文件保持同步?
配置的同步涉及到两个方面:第一,对原始的配置文件实施监控并在其发生变化之后从新加载配置;第二,配置重新加载之后及时通知应用程序进而使后者能够使用最新的配置.接下来我们利用一个简单的.NET Core控 ...
- FYI-django数据库操作-外键
我先定义两个模型,一个是作者,一个是作者出版的书籍,算是一对多的类型. class Person(models.Model); name = models.CharField('作者姓名', ma ...
- vue.js 错误提示bash: vue: command not found
在使用 vue init webpack vue-demo 进行demo的下载时,提示vue: command not found,原因是环境变量没有进行配置,所以会出现这个问题,解决办法 找到你安装 ...
- COGS2287 [HZOI 2015]疯狂的机器人
[题目描述] 现在在二维平面内原点上有一只机器人 他每次操作可以选择向右走,向左走,向下走,向上走和不走(每次如果走只能走一格) 但是由于本蒟蒻施展的大魔法,机器人不能走到横坐标是负数或者纵坐标是负数 ...
- TCP与虚连接
http://bbs.csdn.net/topics/390262738 在TCP通信时,会建立一个从源端到目的端的虚拟连接.感觉这种连接类似电路交换,只是这种连接是虚拟存在的.发送的报文都应该是沿着 ...
- 如何提高mysql的安全性?
1.如果 MySQL 客户端和服务器端的连接需要跨越并通过不可信任的网络,那么需要使用 ssh 隧道来加密该连接的通信.2.使用 set password 语句来修改用户的密码,先“mysql -u ...
- P1266 速度限制
P1266 速度限制 第一次接触这种分层spfa 类似于dp 个人理解 #include<cstdio> #include<iostream> #include<algo ...
- js将数字转换成中文
var _change = { ary0:["零", "一", "二", "三", ...
- JSON 与 XML 的比较 - iOS
在与 web 服务进行数据交换的时候,通常支持两种主要的数据格式(即:JavaScript 对象表示法 JSON 与可扩展标记语言 XML),两者在可读性上都不分高下,接下来对此进行简单的总结和分析, ...
- Win10下安装zookeeper
Win10下安装zookeeper 注册中心zookeeper的安装 0.去Apache官网下载zookeeper http://zookeeper.apache.org/ 2.找到解压路径的conf ...