《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’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- PHP : 数据库中int类型保存时间并通过年月份时分秒进行显示
1.表设计: 2.数据库操作页面:将时间戳插入到数据库中 我们到数据库中可以看到: 3.我们将数据进行显示: 页面结果:(二维数组) 4.以为用mysqli_fetch_all得到的是二维数组,那么我 ...
- 如何迅速掌握并提高linux运维技能(收藏文)
如何迅速掌握并提高linux运维技能 文章来源于南非蚂蚁 之前曾经写过一篇如何学习Linux的文章,得到了很多反馈,大家都在分享自己的学习经验和体会,并且也提出了不少意见和建议.学习这个事情其 ...
- 抽象类和final
抽象类: 概念:在继承过程中,形成一个继承金字塔,位于金字塔底部的类越来越具体(强大),位于塔顶的越来越抽象(简单). 关键字 :abstract 抽象类特性: [1]抽象类过于抽象,实例化后无语义 ...
- BZOJ 3235: [Ahoi2013]好方的蛇
BZOJ 3235: [Ahoi2013]好方的蛇 标签(空格分隔): OI-BZOJ OI-DP OI-容斥原理 Time Limit: 10 Sec Memory Limit: 64 MB Des ...
- 【转】VMware虚拟机系统无法上网怎么办?
有很多用户通过安装VMware软件来创建虚拟机系统,其中就有部分用户在创建好虚拟机系统后遇到无法上网的问题,下面PC6苹果网小编就给大家带来VMware虚拟机系统下无法上网的解决办法: 1.在虚拟机右 ...
- fluent Python
1.1 Python风格的纸牌 Python collections模块中的内置模块:namedtuple https://www.liaoxuefeng.com/wiki/0013747381250 ...
- django.template.exceptions.TemplateSyntaxError: 'article_tags' is not a registered tag library.
django.template.exceptions.TemplateSyntaxError: 'article_tags' is not a registered tag library. Must ...
- System.Web.Caching.Cache
此类是利用缓存来保存信息的.可以把一些稳定的数据,不会随用户而改变的信息利用Cache保存起来,可以优化网站的速度. Cache辅助类已上传:GitHub Cache和Session,cookie的区 ...
- 什么是 BIND 变量?
变量绑定会使联机事务处理过程(OLTP)系统数据库中的SQL执行速度飞快,内存效率极高:不使用绑定变量可能会使OLTP数据库不堪重负,资源被SQL解析严重耗尽,系统运行缓慢. 当一个用户与数据库建立连 ...
- DB总结1
DBA 重构 data new york committee cobol codasyl journal DDL DML 关系演算 域关系演算语言(QBE) 元祖关系演算语言 ...